razorblazormauiskiasharpskia

SkiaSharp in Blazor for desktop app without WASM ( Blazor Hybrid? )


I want to use SkiaSharp in a Blazor desktop app ( Windows, Mac, Linux, etc. )

I know about Blazor Hybrid ( Blazor Desktop ) but I didn't find any way to integrate SkiaSharp.

So basically what I want is a way to use SkiaSharp in a Blazor desktop app but without WASM. Does such a thing exist?


Solution

  • Part 1, Short Answer: Blazor Hybrid does not compile to WASM.
    Reference: .Net 8.0 / Blazor Hybrid / full access to native capabilities:

    Blazor Hybrid apps have full access to native client API capabilities via .NET native app frameworks. In Blazor Hybrid apps, Razor components run directly in the native app, not on WebAssembly.

    Part 1, Longer Answer:

    BlazorWebView:

    .Net Maui BlazorWebView is a control that enables you to host a Blazor web app in your .NET MAUI app. ...
    The Razor components run natively in the .NET process and render web UI to an embedded web view control.

    Compare this to using Blazor with ASP.NET for a client/server web setup.
    "Razor components run natively" is like "server rendering" in ASP.NET Core. Razor's C# code runs just like any other C# code in your app: JIT-compiled (or AOT pre-compiled) to the device's assembly language.

    "embedded web view control" acts as the "browser client". Razor sends it HTML representing current state of the page.

    The "web view control" is a "native control".
    E.g. on a mobile device, it is provided by the device's OS (Android or iOS).

    MAUI Blazor is capable of overlaying on one screen, both "native controls" (such as that web view), and "MAUI controls".

    "MAUI controls" here refers to MAUI / User Interface / Controls; the cross-platform UI that is the evolution of Xamarin Forms.


    Part 2:

    In Maui Blazor, SkiaSharp DOES NOT run in the Blazor part of Maui. That is, it is not run inside the device's "web view control".

    Doing so would require running WASM in that control. This is not how Blazor Hybrid works.

    CONSEQUENCES:


    OBSOLETE

    Short Answer: No; Not Possible. (Unless I am fundamentally wrong about some detail below.)

    Long Answer:


    SkiaSharp in MAUI

    Your app runs SkiaSharp in the NOT BLAZOR part of Maui.
    That won't be inside a Blazor Component.

    If you've laid out a Maui screen (window) such that Blazor WebView is only part of what is showing, SkiaSharp could be somewhere on the screen; just not integrated into Blazor.

    Its even possible to OVERLAY other Maui controls on top of the Blazor View. Your SKCanvasView will be one of the Maui controls. But the Maui controls and the Blazor content won't be aware of each other's location on the screen.


    Use SkiaSharp in Maui

    .NET MAUI - SkiaSharp code sample

    using SkiaSharp.Views.Maui.Controls.Hosting;
    
    builder
    .UseMauiApp<App>()
    .UseSkiaSharp()    // <-- add this line
    ...
    

    In XAML:

    <ContentPage ...
        xmlns:skia="clr-namespace:SkiaSharp.Views.Maui.Controls;assembly=SkiaSharp.Views.Maui.Controls"
        x:Class=...>
    ...
        <skia:SKCanvasView ... PaintSurface="OnYourPaintThisSurface" ... />
    

    In c# code-behind:

    using SkiaSharp;
    using SkiaSharp.Views.Maui.Controls;
    ...
    
        private void OnYourPaintThisSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            ... // your paint code here
        }