This repository contains source code for the integration of Avalonia and MAUI frameworks, supporting the following scenarios:
Supported OS: iOS and Android only.
Demo project to try: https://github.com/AvaloniaUI/AvaloniaMauiHybrid/tree/main/MauiSample
UseAvalonia
call:
builder
.UseMauiApp<App>() // MAUI Application
.UseAvalonia<AvaloniaApp>() // Avalonia Application
UseAvalonia
method.<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" VerticalOptions="Start">
<maui:AvaloniaView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<ava:Button Content="Avalonia Button"/>
</maui:AvaloniaView>
<Button Text="Maui Button"/>
</StackLayout>
Don't forget to add Avalonia xmlns namespaces to your MAUI XAML file xmlns:maui="clr-namespace:Avalonia.Maui.Controls;assembly=Avalonia.Maui"
and xmlns:ava="clr-namespace:Avalonia.Controls;assembly=Avalonia.Controls"
, if IDE didn't include it automatically.
Demo project to try: https://github.com/AvaloniaUI/AvaloniaMauiHybrid/tree/main/AvaloniaSample
<UseMaui>true</UseMaui>
to every project (shared and platform-specific) from where you will use MAUI APIs. For example, here and here.MainActivity
(Android project) and AppDelegate
(iOS project) app builders to include .UseMaui()
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
{
return base.CustomizeAppBuilder(builder)
.UseMaui<AvaloniaSample.Maui.MauiApplication>(this);
}
UseMaui
method. For example, we enable third-party MAUI controls this way.<UniformGrid Columns="2" Height="40">
<Button Content="Avalonia Button" />
<controls:MauiControlHost>
<mauiControls:Button Text="MAUI Button" />
</controls:MauiControlHost>
</UniformGrid>
Don't forget about xmlns namespaces here as well: xmlns:controls="using:Avalonia.Maui.Controls"
and xmlns:mauiControls="using:Microsoft.Maui.Controls"
<UseMauiEssentials>true</UseMauiEssentials>
to your csproj file as well.private async void Button_OnClick(object? sender, RoutedEventArgs e)
{
var location = await Geolocation.GetLocationAsync();
Console.WriteLine(location.ToString());
}
Don't forget about enabling specific permissions. In the case of Geolocation class, you can follow this documentation from Microsoft.