Forum Libraries, Components, and Plugins


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

SKCanvasView size property contradiction


I have a Xamarin.Forms v2.3.3.180 PCL XAML multi-project/platform solution on visual studio 2015/win10, which utilizes SkiaSharp v1.56.1. It seems like there is a contradiction about size info of the SKCanvasView, as I will try to describe below.

XAML structure is ContentPage \ ScrollView \ Grid \ SKCanvasView:
<views:SKCanvasView Grid.Row="8" Grid.Column="0" Grid.ColumnSpan="3" x:Name="canvasview" PaintSurface="OnPaintSchematic" HeightRequest="100" VerticalOptions="Start" HorizontalOptions="Fill" />

I have below code in OnPaintSchematic event handler (e is of type SKPaintSurfaceEventArgs):
canvas.DrawText("Info.ColorType=" + e.Info.ColorType.ToString(), 10, e.Info.Height - 70, paint); canvas.DrawText("Info.WidthxHeight=" + e.Info.Width.ToString() + "x" + e.Info.Height.ToString(), 10, e.Info.Height - 40, paint); canvas.DrawText("SKCanvasView.WidthxHeight=" + canvasview.Width.ToString() + "x" + canvasview.Height.ToString(), 10, e.Info.Height - 10, paint);

This code generates below output:

On UWP (win10 64-bit):

On Android hardware (HTC One, API 21):

On Android emulator (Galaxy Nexus, API 19):

It seems like, on different platforms, two property sets give different sizes. Setting SKCanvasView's HeightRequest at code, rather than or together with setting XAML property, doesn't make any difference. By visual inspection, I can say Info.WidthxHeight is the right one. But I cannot understand the mechanics behind SKCanvasView.WidthxHeight values, which is same or half or one-third of Info.WidthxHeight. Main problem is I cannot set SKCanvasView's real-drawn height (e.Info.Height) by setting HeightRequest property. Any explanation and correction is appreciated.


Best Answer


  • OK. So I ended up with below solution since it's simple and adequate for my case.

    private void OnPaintSchematic(object sender, SKPaintSurfaceEventArgs e) { // desired height of canvas is 100 pixels, convert it to device independent units double dCanvasHeight = 100.0 * canvasview.Height / e.Info.Height; canvasview.HeightRequest = dCanvasHeight; ... }

    Thank you =)

Sign In or Register to comment.