From a106876453a8b0821fbf10e7ed528f302d588fe4 Mon Sep 17 00:00:00 2001 From: Nick Randolph Date: Fri, 12 Apr 2024 00:17:02 +1000 Subject: [PATCH] chore: Fixing reference to UIWindow --- .../MauiEmbedding/Directory.Packages.props | 10 +-- .../MauiEmbedding.apple.cs | 75 ++++++++++--------- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/samples/MauiEmbedding/Directory.Packages.props b/samples/MauiEmbedding/Directory.Packages.props index 7c2df978ee..d786f5db05 100644 --- a/samples/MauiEmbedding/Directory.Packages.props +++ b/samples/MauiEmbedding/Directory.Packages.props @@ -15,17 +15,17 @@ - + - - - + + + - + diff --git a/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs b/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs index 260526e2e8..7e5632e9bd 100644 --- a/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs +++ b/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs @@ -1,44 +1,49 @@ -using UIKit; -using Uno.Extensions.Maui.Platform; -using Microsoft.Maui; -using Windows.UI.Core; - namespace Uno.Extensions.Maui; partial class MauiEmbedding { - // NOTE: This is meant to help initialize MauiEmbedding similar to MauiUIApplicationDelegate - // https://github.com/dotnet/maui/blob/ace9fe5e7d8d9bd16a2ae0b2fe2b888ad681433e/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs#L36-L70 - private static MauiAppBuilder RegisterPlatformServices(this MauiAppBuilder builder, Application app) - { - builder.Services.AddSingleton(sp => - { - var window = sp.GetRequiredService(); - // The _window field is the only way to grab the underlying UIWindow from inside the CoreWindow - // https://github.com/unoplatform/uno/blob/34a32058b812a0a08e658eba5e298ea9d258c231/src/Uno.UWP/UI/Core/CoreWindow.iOS.cs#L17 - var internalWindow = typeof(CoreWindow).GetField("_window", BindingFlags.Instance | BindingFlags.NonPublic); - if(internalWindow is null) - { - throw new MauiEmbeddingException(Properties.Resources.MissingWindowPrivateField); - } - var uiwindow = internalWindow?.GetValue(window.CoreWindow) as UIWindow; - return uiwindow!; - }) - .AddSingleton(sp => sp.GetRequiredService()); + // NOTE: This is meant to help initialize MauiEmbedding similar to MauiUIApplicationDelegate + // https://github.com/dotnet/maui/blob/ace9fe5e7d8d9bd16a2ae0b2fe2b888ad681433e/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs#L36-L70 + private static MauiAppBuilder RegisterPlatformServices(this MauiAppBuilder builder, Application app) + { + builder.Services.AddSingleton(sp => + { + var window = sp.GetRequiredService(); + + // In 5.2 there's a public WindowHelper.GetNativeWindow method but need to call via reflection to maintain support for + // pre 5.2 versions of Uno + var nativeWindowProp = window.GetType().GetProperty("NativeWindow", BindingFlags.Instance | BindingFlags.NonPublic); + if (nativeWindowProp is not null) + { + var nativeWindow = nativeWindowProp.GetValue(window) as UIWindow; + return nativeWindow!; + } + + // The _window field is the only way to grab the underlying UIWindow from inside the CoreWindow + // https://github.com/unoplatform/uno/blob/34a32058b812a0a08e658eba5e298ea9d258c231/src/Uno.UWP/UI/Core/CoreWindow.iOS.cs#L17 + var internalWindow = typeof(CoreWindow).GetField("_window", BindingFlags.Instance | BindingFlags.NonPublic); + if (internalWindow is null) + { + throw new MauiEmbeddingException(Properties.Resources.MissingWindowPrivateField); + } + var uiwindow = internalWindow?.GetValue(window.CoreWindow) as UIWindow; + return uiwindow!; + }) + .AddSingleton(sp => sp.GetRequiredService()); - return builder; - } + return builder; + } - private static void InitializeMauiEmbeddingApp(this MauiApp mauiApp, Application app) - { - var rootContext = new MauiContext(mauiApp.Services); - rootContext.InitializeScopedServices(); + private static void InitializeMauiEmbeddingApp(this MauiApp mauiApp, Application app) + { + var rootContext = new MauiContext(mauiApp.Services); + rootContext.InitializeScopedServices(); - var iApp = mauiApp.Services.GetRequiredService(); + var iApp = mauiApp.Services.GetRequiredService(); - Microsoft.Maui.ApplicationModel.Platform.Init(() => mauiApp.Services.GetRequiredService().RootViewController!); - _ = new EmbeddedApplication(mauiApp.Services, iApp); - app.SetApplicationHandler(iApp, rootContext); - InitializeApplicationMainPage(iApp); - } + Microsoft.Maui.ApplicationModel.Platform.Init(() => mauiApp.Services.GetRequiredService().RootViewController!); + _ = new EmbeddedApplication(mauiApp.Services, iApp); + app.SetApplicationHandler(iApp, rootContext); + InitializeApplicationMainPage(iApp); + } }