AvaloniaUI/AvaloniaMauiHybrid

"Inverse" Avalonia Hybrid

profix898 opened this issue · 3 comments

Are there any plans for an inverse integration, i.e. using Maui "components" in Avalonia?

I'm not talking about the views/controls (although that might be desirable for some people as well, like NativeHost for native controls).

From my point of view, there are especially two fields where Avalonia UI would benefit from MAUI:

  1. Use IDrawable from MAUI
    Microsoft.Maui.Graphics (https://learn.microsoft.com/en-us/dotnet/maui/user-interface/graphics/) provides a 2D graphics abstraction (ICanvas). Since it already has a Skia backend, it should be fairly simple to provide an Avalonia control (counterpart to GraphicsView in MAUI, see https://learn.microsoft.com/de-de/dotnet/maui/user-interface/controls/graphicsview) which enables drawing an MAUI IDrawable. I know that Avalonia has its own drawing API (DrawingContext), but the ability to reuse drawing code from .NET MAUI would be quite welcome ;)

  2. Use platform-specific APIs (Essentials) from MAUI
    What is the solution for accessing platform-specific functionality (e.g. GPS, Vibration, Clipboard, etc.) in Avalonia UI?
    In .NET MAUI there are platform integration APIs (https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/). Can we use those directly for Avalonia as well. Is there any builder support (host-builder pattern) that integrates MAUI platform APIs with Avalonia?
    Or is there any Avalonia-specific alternative available?

I haven't dig deeply into it, but it should be possible with NativeControlHost.
Avalonia is able to host any native control inside, you just need to pass a handle. Keeping in mind nature of how MAUI works, there shouldn't problems.
See these examples to see what I mean:
https://github.com/AvaloniaUI/Avalonia/blob/master/samples/ControlCatalog.Android/EmbedSample.Android.cs
https://github.com/AvaloniaUI/Avalonia/blob/master/samples/ControlCatalog.iOS/EmbedSample.iOS.cs

https://github.com/AvaloniaUI/Avalonia/blob/master/samples/ControlCatalog/Pages/NativeEmbedPage.xaml.cs#L56-L77

Actually, I was thinking not to use the full MAUI stack, but just selected parts of it (as mentioned above).

Here is a sample repo with a MauiGraphicsControl that uses Skia canvas directly to render any MAUI IDrawable in Avalonia: https://github.com/profix898/AvaloniaApplication1. Its my first week with Avalonia (but did some MAUI stuff before), so let me know if this is stupid somehow ;)

Control:
AvaloniaApplication1/MauiGraphicsControl.cs
Sample IDrawable:
AvaloniaApplication1/Maui/DrawMe.cs

Sample usage in
ViewModels\MainViewModel.cs
Views\MainView.axaml

I want to check Avalonia builder and integration with MAUI essentials (for non-UI platform features) next.

This repository now has MauiControlHost avalonia control implementation that allows hosting MAUI controls.
While it was possible for a while now, it was quite tricky to wire two frameworks together in this way.
Will update the readme soon to add some "quick start" steps.

Essentials and customs maui controls should also work.