Skip to content

Commit

Permalink
Simplify basic scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkatz6 committed Sep 21, 2023
1 parent 432d015 commit 5c46c1b
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Avalonia.Maui/Avalonia.Maui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<!-- Packaging -->
<PropertyGroup>
<IsPackable>true</IsPackable>
<Version>11.0-preview1</Version>
<Version>11.0-preview2</Version>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/AvaloniaUI/AvaloniaMauiHybrid</RepositoryUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
Expand Down
13 changes: 8 additions & 5 deletions Avalonia.Maui/AvaloniaAppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ namespace Avalonia.Maui;
public static class AvaloniaAppBuilderExtensions
{
#if ANDROID
public static AppBuilder WithMaui<TMauiApplication>(this AppBuilder appBuilder, global::Android.App.Activity activity, Action<MauiAppBuilder>? configure = null)
public static AppBuilder UseMaui<TMauiApplication>(this AppBuilder appBuilder, global::Android.App.Activity activity, Action<MauiAppBuilder>? configure = null)
where TMauiApplication : Microsoft.Maui.Controls.Application
#elif IOS
public static AppBuilder UseMaui<TMauiApplication>(this AppBuilder appBuilder, IUIApplicationDelegate applicationDelegate, Action<MauiAppBuilder>? configure = null)
where TMauiApplication : Microsoft.Maui.Controls.Application
#else
public static AppBuilder WithMaui<TMauiApplication>(this AppBuilder appBuilder, Action<MauiAppBuilder>? configure = null)
public static AppBuilder UseMaui<TMauiApplication>(this AppBuilder appBuilder, Action<MauiAppBuilder>? configure = null)
where TMauiApplication : Microsoft.Maui.Controls.Application
#endif
{
Expand All @@ -41,8 +44,8 @@ public static AppBuilder WithMaui<TMauiApplication>(this AppBuilder appBuilder,
.AddSingleton<global::Android.Content.Context>(activity)
.AddSingleton(activity)
#elif IOS
.AddSingleton(UIApplication.SharedApplication.Delegate)
.AddSingleton<UIWindow>(_ => UIApplication.SharedApplication.Delegate.GetWindow())
.AddSingleton(applicationDelegate ?? UIApplication.SharedApplication.Delegate)
.AddSingleton<UIWindow>(static p => p.GetService<IUIApplicationDelegate>()!.GetWindow())
#endif
.AddSingleton<IMauiInitializeService, MauiEmbeddingInitializer>();
Expand All @@ -69,7 +72,7 @@ private static void InitializeMauiEmbeddingApp(this MauiApp mauiApp)
var rootContext = new MauiContext(mauiApp.Services);
var services = mauiApp.Services;
#if IOS
var platformApplication = UIApplication.SharedApplication.Delegate;
var platformApplication = mauiApp.Services.GetRequiredService<IUIApplicationDelegate>();
var window = platformApplication.GetWindow();
if (window == null)
{ // hack for older Avalonia versions.
Expand Down
16 changes: 13 additions & 3 deletions Avalonia.Maui/MauiAppHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
using System;
using Avalonia.Maui.Controls;
using Avalonia.Maui.Handlers;
using Microsoft.Maui.Hosting;

namespace Avalonia.Maui
{
public static class MauiAppHostBuilderExtensions
{
public static MauiAppBuilder UseAvalonia<TApp>(this MauiAppBuilder builder, Action<AppBuilder> customizeBuilder) where TApp : Application, new()
public static MauiAppBuilder UseAvalonia<TApp>(this MauiAppBuilder builder, Action<AppBuilder>? customizeBuilder = null) where TApp : Application, new()
{
var avaloniaBuilder = AppBuilder.Configure<TApp>();
customizeBuilder(avaloniaBuilder);
#if ANDROID
avaloniaBuilder.UseAndroid();
#elif IOS
avaloniaBuilder.UseiOS();
#endif

customizeBuilder?.Invoke(avaloniaBuilder);

avaloniaBuilder.SetupWithoutStarting();
return builder;

return builder
.ConfigureMauiHandlers(handlers =>
{
#if ANDROID || IOS
handlers.AddHandler(typeof(AvaloniaView), typeof(AvaloniaViewHandler));
#endif
});;
}
}
}
2 changes: 1 addition & 1 deletion AvaloniaSample/AvaloniaSample.Android/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class MainActivity : AvaloniaMainActivity<App>
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
{
return base.CustomizeAppBuilder(builder)
.WithMaui<AvaloniaSample.Maui.MauiApplication>(this, b => b
.UseMaui<AvaloniaSample.Maui.MauiApplication>(this, b => b
.ConfigureSyncfusionCore()
.UseMauiCommunityToolkit()
.UseMauiCommunityToolkitMediaElement());
Expand Down
2 changes: 1 addition & 1 deletion AvaloniaSample/AvaloniaSample.iOS/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
AVAudioSession.SharedInstance().SetCategory(AVAudioSessionCategory.Playback);

return base.CustomizeAppBuilder(builder)
.WithMaui<AvaloniaSample.Maui.MauiApplication>(b => b
.UseMaui<AvaloniaSample.Maui.MauiApplication>(this, b => b
.ConfigureSyncfusionCore()
.UseMauiCommunityToolkit()
.UseMauiCommunityToolkitMediaElement());
Expand Down
1 change: 1 addition & 0 deletions AvaloniaSample/AvaloniaSample/AvaloniaSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<UseMaui>true</UseMaui>
<UseMauiEssentials>true</UseMauiEssentials>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
Expand Down
5 changes: 0 additions & 5 deletions MauiSample/AvaloniaApp.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ public override void Initialize()
AvaloniaXamlLoader.Load(this);
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}

public override void OnFrameworkInitializationCompleted()
{
base.OnFrameworkInitializationCompleted();
Expand Down
6 changes: 0 additions & 6 deletions MauiSample/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ public static MauiApp CreateMauiApp()
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
})
.ConfigureMauiHandlers(handlers =>
{
#if ANDROID || IOS
handlers.AddHandler(typeof(AvaloniaView), typeof(AvaloniaViewHandler));
#endif
});

return builder.Build();
Expand Down

0 comments on commit 5c46c1b

Please sign in to comment.