Bewerken

Delen via


Reuse SkiaSharp code in .NET MAUI

SkiaSharp is a 2D graphics system for .NET and C# that draws 2D vector graphics, bitmaps, and text. It's powered by the open-source Skia graphics engine that's used extensively in Google products. You can reuse SkiaSharp code from your Xamarin.Forms apps in your .NET Multi-platform App UI (.NET MAUI) apps with some minor updates.

To reuse your SkiaSharp code from a Xamarin.Forms app in a .NET MAUI app, you must:

  • Remove the Xamarin.Forms SkiaSharp NuGet packages from your project, and add the .NET MAUI SkiaSharp NuGet packages to your project.
  • Update namespaces.
  • Initialize SkiaSharp.

Add NuGets

SkiaSharp for .NET MAUI is packaged as a series of NuGet packages. After you've migrated your Xamarin.Forms app to a .NET MAUI app, you should remove all the existing SkiaSharp NuGet packages from your app. Then, use the NuGet package manager to search for the SkiaSharp.Views.Maui.Controls NuGet package and add it to your project. This will also install dependent SkiaSharp packages.

Update namespaces

Xamarin.Forms apps that use SkiaSharp typically use types from the SkiaSharp namespace and the SkiaSharp.Views.Forms namespace. In SkiaSharp for .NET MAUI, you'll continue to use the SkiaSharp namespace but the types that were in the SkiaSharp.Views.Forms namespace have moved into the SkiaSharp.Views.Maui and SkiaSharp.Views.Maui.Controls namespaces.

The following table shows the namespaces you'll need to use to build your SkiaSharp code in a .NET MAUI app:

.NET MAUI namespace Details
SkiaSharp Contains all the SkiaSharp classes, structures, and enumerations.
SkiaSharp.Views.Maui Contains types to support touch interactions, and event arguments.
SkiaSharp.Views.Maui.Controls Contains the SKCanvasView class, which derives from the .NET MAUI View class and hosts your SkiaSharp graphics output. Also contains different ImageSource classes.
SkiaSharp.Views.Maui.Controls.Hosting Contains the UseSkiaSharp method that's used to initialize SkiaSharp in your .NET MAUI app. For more information, see Initialize SkiaSharp.

Initialize SkiaSharp

Initialize SkiaSharp in your app by calling the UseSkiaSharp method on the MauiAppBuilder object in your MauiProgram class:

using Microsoft.Extensions.Logging;
using SkiaSharp.Views.Maui.Controls.Hosting;

namespace MyMauiApp;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .UseSkiaSharp()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });

        #if DEBUG
        builder.Logging.AddDebug();
        #endif

        return builder.Build();
    }
}

Note

Calling the UseSkiaSharp method requires you to add a using directive for the SkiaSharp.Views.Maui.Controls.Hosting namespace.