NET MAUI and SkiaSharp Integration

Yusuf 791 Reputation points
2023-10-18T13:59:59.7833333+00:00

Hello fellow developers,

I hope you're all doing well. I'm currently working on a .NET MAUI project and I'm facing some issues with integrating SkiaSharp into my application. I've followed the usual steps, but my app is getting stuck with an unhandled exception in the WinUI layer.

Here's what I've done so far:

  • Created a new .NET MAUI app.
  • Added the SkiaSharp.Views.Maui.Controls and SkiaSharp.Views.Maui.Core libraries.
  • Set up the XAML layout with a SKCanvasView.
  • Added a PaintSurface event handler.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:skia="clr-namespace:SkiaSharp.Views.Maui.Controls;assembly=SkiaSharp.Views.Maui.Controls"
             x:Class="MauiApp5.MainPage">

    <StackLayout>
        <skia:SKCanvasView
     x:Name="skiaView"
     PaintSurface="OnPaintSurface"/>

    </StackLayout>
</ContentPage>

namespace MauiApp5
{
    public partial class MainPage : ContentPage
    {

        public MainPage()
        {
            InitializeComponent();
        }

        private void OnPaintSurface(object sender, SkiaSharp.Views.Maui.SKPaintSurfaceEventArgs e)
        {

        }
    }
}

However, when I run the app, it crashes with the following message:

MauiApp5.dll!MauiApp5.WinUI.App.InitializeComponent.AnonymousMethod__3_1(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e) Line 68 C#

I've checked my SkiaSharp version, ensured the environment is set up correctly, and double-checked my code for any errors. Still, I can't figure out what's causing the issue.

Has anyone encountered a similar problem or could provide guidance on what might be going wrong? Any help or insights would be greatly appreciated.

Thank you in advance!

Developer technologies | .NET | Xamarin
Developer technologies | .NET | .NET MAUI
Developer technologies | C#
0 comments No comments
{count} vote

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2023-10-19T03:17:53.09+00:00

    Hello,

    SkiaSharp in MAUI requires the use of SkiaSharp to be declared in builder.

    builder
        .UseMauiApp<App>()
        .UseSkiaSharp()
    

    In addition, for drawing controls, it is recommended that you use the official built-in GraphicsView.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.