Draw points with Skiasharp after receiving coordinates from Messaging Center

Iban_MSIban_MS Member ✭✭

Greetings,

Currently, I am developing a Xamarin Forms (tabbed page) app to draw points in a graph after receiving their coordinates via Bluetooth.

The Bluetooth communication logic is implemented in the Android OS part of the project, while all the UI has been developed in the crossplatform part.

So far, I have been able to print the values of the coordinates in one of the pages after sending and receiving them with MessagingCenter.

However, when I send the coordinates to the page in which the points must be plotted via SkiaSharp, nothing seems to be received from MessagingCenter.

This is what I'm trying to do in the xaml.cs of the mentioned page:

namespace TestBth
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LissajousPage : ContentPage
        {

        SKPaint PointPaint = new SKPaint
        {

            Style = SKPaintStyle.Stroke,
            Color = Color.Yellow.ToSKColor(),
            StrokeWidth = 1,
            IsAntialias = true

        };

        public LissajousPage()
        {
            InitializeComponent();

            Device.StartTimer(TimeSpan.FromMilliseconds(100), () =>
            {                                           

                canvasView.InvalidateSurface();

                return true;

            });
        }


        private void OnCanvasViewPaintSurface(object sendr, SKPaintSurfaceEventArgs e)
        {
            SKImageInfo info = e.Info;
            SKSurface surface = e.Surface;
            SKCanvas canvas = surface.Canvas;


            int width = e.Info.Width;
            int height = e.Info.Height;


            //Set transforms
            canvas.Translate(width / 2, height / 2);
            canvas.Scale(width / 200f);

            //Drawings  

            canvas.Clear();

            byte[] coord = new byte[200];

            MessagingCenter.Subscribe<App, byte[]>((App)Application.Current, "Points", (sender, arg) =>
            {
                MessagingCenter.Unsubscribe<App, byte[]>((App)Application.Current, "Points");

                coord = arg;

            });                       

            for (int i = 0; i == 199; i++)
            {                
                canvas.DrawPoint(coord[i], coord[i+1], PointPaint);
            }

        }

    }
}

And this is the SEND MessagingCenter statement in the Android part, which is executed right after some coordinates are received via Bluetooth:

Xamarin.Forms.MessagingCenter.Send<App, byte[]>((App)Xamarin.Forms.Application.Current, "Points", points);

I have also tried to send and receive a simple boolean, but MessagingCenter just doesn't seem to work in this page.

Maybe I'm not placing the SUBSCRIBE statement correctly?

Any suggestions?

Thank you in advance.

Sign In or Register to comment.