Skip to content

Commit

Permalink
Some reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkatz6 committed Oct 12, 2023
1 parent e544623 commit 84f1dc7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 38 deletions.
64 changes: 32 additions & 32 deletions Avalonia.Maui/AvaloniaAppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Avalonia.Maui;
public static class AvaloniaAppBuilderExtensions
{
#if ANDROID
public static AppBuilder UseMaui<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)
Expand All @@ -35,39 +35,39 @@ public static AppBuilder UseMaui<TMauiApplication>(this AppBuilder appBuilder, A
return appBuilder
.AfterSetup(appBuilder =>
{
var builder = MauiApp.CreateBuilder()
.UseMauiEmbedding<TMauiApplication>();
var builder = MauiApp.CreateBuilder()
.UseMauiEmbedding<TMauiApplication>();
builder.Services.AddSingleton(appBuilder.Instance!)
#if ANDROID
.AddSingleton(activity.Application!)
.AddSingleton<global::Android.Content.Context>(activity)
.AddSingleton(activity)
.AddSingleton(activity.Application!)
.AddSingleton<global::Android.Content.Context>(activity)
.AddSingleton(activity)
#elif IOS
.AddSingleton(applicationDelegate ?? UIApplication.SharedApplication.Delegate)
.AddSingleton<UIWindow>(static p => p.GetService<IUIApplicationDelegate>()!.GetWindow())
#endif
.AddSingleton<IMauiInitializeService, MauiEmbeddingInitializer>();
configure?.Invoke(builder);
var mauiApp = builder.Build();
InitializeMauiEmbeddingApp(mauiApp);
});
}

private static void InitializeMauiEmbeddingApp(this MauiApp mauiApp)
{
var iApp = mauiApp.Services.GetRequiredService<IApplication>();
var iApp = mauiApp.Services.GetRequiredService<IApplication>();

#if ANDROID
var window = mauiApp.Services.GetRequiredService <global::Android.App.Activity>();
var scope = mauiApp.Services.CreateScope();
var platformApplication = window.Application!;
var services = scope.ServiceProvider;
var rootContext = new MauiContext(scope.ServiceProvider, window);
var window = mauiApp.Services.GetRequiredService<global::Android.App.Activity>();
var scope = mauiApp.Services.CreateScope();
var platformApplication = window.Application!;
var services = scope.ServiceProvider;
var rootContext = new MauiContext(scope.ServiceProvider, window);

Microsoft.Maui.ApplicationModel.Platform.Init(window, null);
Microsoft.Maui.ApplicationModel.Platform.Init(window, null);
#else
var rootContext = new MauiContext(mauiApp.Services);
var services = mauiApp.Services;
Expand All @@ -87,28 +87,28 @@ private static void InitializeMauiEmbeddingApp(this MauiApp mauiApp)
#endif
#endif

var scopedServices = rootContext.Services.GetServices<IMauiInitializeScopedService>();
foreach (var service in scopedServices)
{
service.Initialize(rootContext.Services);
}
var scopedServices = rootContext.Services.GetServices<IMauiInitializeScopedService>();
foreach (var service in scopedServices)
{
service.Initialize(rootContext.Services);
}

platformApplication.SetApplicationHandler(iApp, rootContext);
IPlatformApplication.Current = new EmbeddingApplication(services, iApp);
platformApplication.SetApplicationHandler(iApp, rootContext);
IPlatformApplication.Current = new EmbeddingApplication(services, iApp);

if (iApp is Microsoft.Maui.Controls.Application { Handler.MauiContext: not null } app
&& iApp.Windows is List<MauiWindow> windows)
{
var virtualWindow = CreateVirtualWindow(app, window);
windows.Add(virtualWindow);
}
if (iApp is Microsoft.Maui.Controls.Application { Handler.MauiContext: not null } app
&& iApp.Windows is List<MauiWindow> windows)
{
var virtualWindow = CreateVirtualWindow(app, window);
windows.Add(virtualWindow);
}
}

private static Window CreateVirtualWindow(Microsoft.Maui.Controls.Application app, PlatformWindow? window)
{
#if ANDROID
var services = app.Handler!.MauiContext!.Services;
var context = new MauiContext(services, services.GetRequiredService<global::Android.App.Activity>());
var services = app.Handler!.MauiContext!.Services;
var context = new MauiContext(services, services.GetRequiredService<global::Android.App.Activity>());
#else
var context = app.Handler.MauiContext;
#endif
Expand All @@ -125,6 +125,6 @@ private static Window CreateVirtualWindow(Microsoft.Maui.Controls.Application ap
virtualWindow.Page = new ContentPage();
return virtualWindow;
}

private record EmbeddingApplication(IServiceProvider Services, IApplication Application) : IPlatformApplication;
}
14 changes: 8 additions & 6 deletions AvaloniaMauiHybrid.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AvaloniaSample", "AvaloniaS
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{31E88003-6C5B-41F7-8A36-CBFE095CA760}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AvaloniaSample", "AvaloniaSample\AvaloniaSample\AvaloniaSample.csproj", "{5B52DC9E-B2D8-444E-8CA7-102FD762675F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AvaloniaSample", "AvaloniaSample\AvaloniaSample\AvaloniaSample.csproj", "{5B52DC9E-B2D8-444E-8CA7-102FD762675F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AvaloniaSample.Android", "AvaloniaSample\AvaloniaSample.Android\AvaloniaSample.Android.csproj", "{3CFDDDE4-C90B-48FF-8C95-50F96909BF5A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AvaloniaSample.Android", "AvaloniaSample\AvaloniaSample.Android\AvaloniaSample.Android.csproj", "{3CFDDDE4-C90B-48FF-8C95-50F96909BF5A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AvaloniaSample.iOS", "AvaloniaSample\AvaloniaSample.iOS\AvaloniaSample.iOS.csproj", "{E92818E3-6BFA-4C9E-9481-3BECBACAFF0A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AvaloniaSample.iOS", "AvaloniaSample\AvaloniaSample.iOS\AvaloniaSample.iOS.csproj", "{E92818E3-6BFA-4C9E-9481-3BECBACAFF0A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -48,24 +48,26 @@ Global
{5B52DC9E-B2D8-444E-8CA7-102FD762675F}.Release|Any CPU.Build.0 = Release|Any CPU
{3CFDDDE4-C90B-48FF-8C95-50F96909BF5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3CFDDDE4-C90B-48FF-8C95-50F96909BF5A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3CFDDDE4-C90B-48FF-8C95-50F96909BF5A}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{3CFDDDE4-C90B-48FF-8C95-50F96909BF5A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3CFDDDE4-C90B-48FF-8C95-50F96909BF5A}.Release|Any CPU.Build.0 = Release|Any CPU
{E92818E3-6BFA-4C9E-9481-3BECBACAFF0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E92818E3-6BFA-4C9E-9481-3BECBACAFF0A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E92818E3-6BFA-4C9E-9481-3BECBACAFF0A}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{E92818E3-6BFA-4C9E-9481-3BECBACAFF0A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E92818E3-6BFA-4C9E-9481-3BECBACAFF0A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2803DA96-B1F5-490A-A82B-82C073DE4767}
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D2634557-8A3C-4062-9480-3C6A5A4CC147} = {31E88003-6C5B-41F7-8A36-CBFE095CA760}
{10E5B37C-47D1-498B-AD9D-9A281064C091} = {31E88003-6C5B-41F7-8A36-CBFE095CA760}
{5B52DC9E-B2D8-444E-8CA7-102FD762675F} = {10E5B37C-47D1-498B-AD9D-9A281064C091}
{3CFDDDE4-C90B-48FF-8C95-50F96909BF5A} = {10E5B37C-47D1-498B-AD9D-9A281064C091}
{E92818E3-6BFA-4C9E-9481-3BECBACAFF0A} = {10E5B37C-47D1-498B-AD9D-9A281064C091}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2803DA96-B1F5-490A-A82B-82C073DE4767}
EndGlobalSection
EndGlobal

0 comments on commit 84f1dc7

Please sign in to comment.