How to bind coordinate bytes of points from another page.

Iban_MSIban_MS Member ✭✭
edited June 5 in SkiaSharp

Greetings,

I am trying to draw some points in my screen by using canvas.DrawPoint().

However, I receive the coordinates of the points in a byte array via Bluetooth and I can't seem to find the way to have access to this data from the Page in which I am drawing. I can access to it from other Pages' ViewModels with MessagingCenter.

Although I have specified a ViewModel for the drawing Page, I don't know how to bind the X and Y coordinates of the points. Would this be the right approach?

This is the behind code of the page in which I draw:

    namespace TestBth
    {
        [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class LissajousPage : ContentPage
        {  
            SKPaint CircleAPaint = new SKPaint
            {
                Style = SKPaintStyle.Stroke,
                Color = Color.White.ToSKColor(),
                StrokeWidth = 0,
                IsAntialias = true
            };

            SKPaint PointPaint = new SKPaint
            {

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

            };      

            public LissajousPage()
            {
                InitializeComponent();

                this.BindingContext = new ParametersViewModel();

                Device.StartTimer(TimeSpan.FromMilliseconds(200), () =>
                {                             
                    canvasView.InvalidateSurface();

                    return true;

                });
            }

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


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


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

                //Drawings  

                canvas.Clear();

                canvas.DrawCircle(0, 0, CircleAradius, CircleAPaint);

                for (int i = 0; i < 198; i=+2)
                {
                    canvas.DrawPoint(points[i],   //BIND THIS
                                     points[i+1], //AND THIS
                                    PointPaint);

                }

            }

        }
    }

And this is the ViewModel that receives the coordinates:

    namespace TestBth
    {   
        public class ParametersViewModel : INotifyPropertyChanged
        {      
            public event PropertyChangedEventHandler PropertyChanged;

            public ParametersViewModel()
            {
                byte[] points = new byte[200]; //THIS IS THE VARIABLE I NEED TO ACCESS FROM THE PREVIOUS PAGE.

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

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

                        Array.ConstrainedCopy(arg, 2, points, 0, 200); 

                    });

                return true;
                });
            }

        }
    }

Thank you in advance for your help.

Best regards.

Sign In or Register to comment.