Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

SKCanvasView Fires Paint Surface repeatedly

$
0
0

I have a list of objects that are being stored in an ObservableCollection. When a particular item is selected from the list, it navigates to a Content page that contains a StackLayout. This contains three children, two of which are labels and a SKCanvasView

StackLayout sl = new StackLayout { Padding = 10 };
sl.Children.Add(new Label {
    Text = "Status: " + PatientStatus.FromValue(patient.Status).ToString(),
    FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label))
});
sl.Children.Add(new Label {
    Text = "Completed: " + patient.Completed,
    FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label))
});
sgl = new SKCanvasView();
sgl.PaintSurface += Sgl_PaintSurface;
sl.Children.Add(sgl);
Content = sl;

On initialization, the two labels are populated with the appropriate value from the passed object (patient). A copy of this is also stored in a global variable within the same class.

Sgl_PaintSurface fires fine, and it goes through the code, however, it continues firing again and again. If it does stop, then the application shows a blank canvas under the two labels. It does not contain any artifacts at all. The background is slightly different then the application, so I can see the outline of the box.

private void Sgl_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
{
    var Status = PatientStatus.FromValue(pt.Status);
    var surface = e.Surface;
    var canvas = surface.Canvas;
    canvas.Clear(SKColors.White);

    var complete = new SKPaint
    {
        IsAntialias = true,
        Style = SKPaintStyle.Fill,
        Color = SKColors.Green,
        StrokeWidth = 5,
        TextSize = 15,
        TextAlign = SKTextAlign.Center
    };
    var active = new SKPaint
    {
        IsAntialias = true,
        Style = SKPaintStyle.Fill,
        Color = SKColors.Green,
        StrokeWidth = 5,
        TextSize = 15,
        FakeBoldText = true,
        TextAlign = SKTextAlign.Center
    };
    var incomplete = new SKPaint
    {
        IsAntialias = true,
        Style = SKPaintStyle.Fill,
        Color = SKColors.Silver,
        StrokeWidth = 5,
        TextSize = 15,
        TextAlign = SKTextAlign.Center
    };
    if (height > width)
        for (int x = 50; x <= (50 * 7); x = x + 50)
        {
            var currentState = Status == PatientStatus.FromOrder((x / 50) - 1) ? active : (Status > PatientStatus.FromOrder((x / 50) - 1) ? complete : incomplete);

            if (x < (50 * 7))
                canvas.DrawLine(100, x, 100, x + 50, currentState == active ? incomplete : currentState);
            canvas.DrawCircle(100, x, 10, currentState);
            canvas.DrawText(PatientStatus.FromOrder((x / 50) - 1).ToString(), 150, x + 3, currentState);
        }
    else
        for (int x = 100; x <= (100 * 7); x = x + 100)
        {
            var currentState = Status == PatientStatus.FromOrder((x / 100) - 1) ? active : (Status > PatientStatus.FromOrder((x / 100) - 1) ? complete : incomplete);

            if (x < (100 * 7))
                canvas.DrawLine(x, 100, x + 100, 100, currentState == active ? incomplete : currentState);
            canvas.DrawCircle(x, 100, 10, currentState);
            int stringLength = PatientStatus.FromOrder((x / 100) - 1).ToString().Length;
            canvas.DrawText(PatientStatus.FromOrder((x / 100) - 1).ToString(), x, 60, currentState);
        }
    //sgl.PaintSurface -= Sgl_PaintSurface;
}

I have this same code in a separate project, and it only fires the event once, which suggests an issue with the page load, but I am not certain. I have also tried to remove the event listener from the object after it executes initially, but that also produces a blank canvas.

I am testing this using an Android VM running in Parallels.

Any ideas on how to resolve this?


Viewing all articles
Browse latest Browse all 204402

Trending Articles