diff --git a/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs b/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs index 0950db62bf..03dbf5c5fa 100644 --- a/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs +++ b/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs @@ -1,5 +1,6 @@ using UIKit; using Uno.Extensions.Maui.Platform; +using Windows.UI.Core; namespace Uno.Extensions.Maui; @@ -9,7 +10,19 @@ partial class MauiEmbedding // 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.AddTransient(sp => sp.GetRequiredService().Window!) + 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()); return builder; @@ -26,7 +39,7 @@ private static void InitializeMauiEmbeddingApp(this MauiApp mauiApp, Application throw new MauiEmbeddingException(Properties.Resources.TheApplicationMustInheritFromEmbeddingApplication); } - Microsoft.Maui.ApplicationModel.Platform.Init(() => app.Window!.RootViewController!); + Microsoft.Maui.ApplicationModel.Platform.Init(() => mauiApp.Services.GetRequiredService().RootViewController!); embeddingApp.InitializeApplication(mauiApp.Services, iApp); app.SetApplicationHandler(iApp, rootContext); InitializeApplicationMainPage(iApp); diff --git a/src/Uno.Extensions.Maui.UI/MauiEmbedding.cs b/src/Uno.Extensions.Maui.UI/MauiEmbedding.cs index 9dd9b1c88a..6a4f0d5440 100644 --- a/src/Uno.Extensions.Maui.UI/MauiEmbedding.cs +++ b/src/Uno.Extensions.Maui.UI/MauiEmbedding.cs @@ -143,7 +143,7 @@ private static void InitializeApplicationMainPage(IApplication iApp) virtualWindow.Handler = new EmbeddedWindowHandler { #if IOS || MACCATALYST - PlatformView = context.Services.GetRequiredService().Window, + PlatformView = context.Services.GetRequiredService(), #elif ANDROID PlatformView = context.Services.GetRequiredService(), #elif WINDOWS diff --git a/src/Uno.Extensions.Maui.UI/Properties/Resources.Designer.cs b/src/Uno.Extensions.Maui.UI/Properties/Resources.Designer.cs index f94dcb8f60..6dfc3c764d 100644 --- a/src/Uno.Extensions.Maui.UI/Properties/Resources.Designer.cs +++ b/src/Uno.Extensions.Maui.UI/Properties/Resources.Designer.cs @@ -78,6 +78,15 @@ internal static string MauiEmbeddingInitializationExceptionMessage { } } + /// + /// Looks up a localized string similar to CoreWindow is missing _window internal field required to access UIWindow, you may need an updated version of the Uno.Extension.Maui.WinUI package.. + /// + internal static string MissingWindowPrivateField { + get { + return ResourceManager.GetString("MissingWindowPrivateField", resourceCulture); + } + } + /// /// Looks up a localized string similar to No value provided for the Maui Color.. /// diff --git a/src/Uno.Extensions.Maui.UI/Properties/Resources.resx b/src/Uno.Extensions.Maui.UI/Properties/Resources.resx index 8881ee8272..538c1aa7aa 100644 --- a/src/Uno.Extensions.Maui.UI/Properties/Resources.resx +++ b/src/Uno.Extensions.Maui.UI/Properties/Resources.resx @@ -123,6 +123,9 @@ You must call UseMauiEmbedding from the Application + + CoreWindow is missing _window internal field required to access UIWindow, you may need an updated version of the Uno.Extension.Maui.WinUI package. + No value provided for the Maui Color.