Can SkiaSharp be used in a WPF Application?
Asked Answered
N

1

5

I know a SkiaSharp canvas can be placed in a Grid in a Xamarin App. Here is some example (working) code:

xmlns:skia="clr-namespace:SkiaSharp.Views.Forms;assembly=SkiaSharp.Views.Forms"
<skia:SKCanvasView 
    x:Name="AUView"
    Grid.Row="2"
    PaintSurface="AUView_PaintSurface" />

But when I try to do a similar thing in a WPF App:

xmlns:skia="clr-namespace:SkiaSharp;assembly=SkiaSharp"
<skia:SKCanvas
   Name="AUView"
   Grid.Row="2"
   PaintSurface="AUView_PaintSurface" />

I get the error:

A value of type 'SKCanvas' cannot be added to a collection or dictionary of type 'UIElementCollection'.

Is it possible to put an SKCanvas in a grid cell in WPF or does the whole of the page have to be an SKCanvas?

Else, how/can you use SkiaSharp in WPF?

Nickelsen answered 29/8, 2020 at 2:12 Comment(1)
T
13

First, you have to install the SkiaSharp.Views.WPF package, which will automatically install SkiaSharp, too. The SkiaSharp package alone will not contain the needed WPF control, which is SKElement.

As it is a FrameworkElement, you can place it anywhere in your view. SKCanvas is not a WPF compatible component. To use an SKElement in your XAML, you have to pull in its XML namespace.

xmlns:skia="clr-namespace:SkiaSharp.Views.WPF;assembly=SkiaSharp.Views.WPF"

Then place the SKCanvas control anywhere in your view.

<skia:SKElement
      Name="AUView"
      Grid.Row="2"
      PaintSurface="AUView_PaintSurface"/>
Tippett answered 29/8, 2020 at 10:2 Comment(1)
Believe it or not, I did not see the ...WPF Nuget package. Now installed and with the help of the examples pointed out earlier, it is all working. Thanks.Nickelsen

© 2022 - 2024 — McMap. All rights reserved.