Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Adding UIWindow to the service container (backport #1832) #1834

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using UIKit;
using Uno.Extensions.Maui.Platform;
using Windows.UI.Core;

namespace Uno.Extensions.Maui;

Expand All @@ -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<UIWindow>(sp => sp.GetRequiredService<Application>().Window!)
builder.Services.AddSingleton<UIWindow>(sp =>
{
var window = sp.GetRequiredService<Microsoft.UI.Xaml.Window>();
// 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<IUIApplicationDelegate>(sp => sp.GetRequiredService<Application>());

return builder;
Expand All @@ -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<UIWindow>().RootViewController!);
embeddingApp.InitializeApplication(mauiApp.Services, iApp);
app.SetApplicationHandler(iApp, rootContext);
InitializeApplicationMainPage(iApp);
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.Extensions.Maui.UI/MauiEmbedding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private static void InitializeApplicationMainPage(IApplication iApp)
virtualWindow.Handler = new EmbeddedWindowHandler
{
#if IOS || MACCATALYST
PlatformView = context.Services.GetRequiredService<Microsoft.UI.Xaml.Application>().Window,
PlatformView = context.Services.GetRequiredService<UIKit.UIWindow>(),
#elif ANDROID
PlatformView = context.Services.GetRequiredService<Android.App.Activity>(),
#elif WINDOWS
Expand Down
9 changes: 9 additions & 0 deletions src/Uno.Extensions.Maui.UI/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Uno.Extensions.Maui.UI/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@
<data name="MauiEmbeddingInitializationExceptionMessage" xml:space="preserve">
<value>You must call UseMauiEmbedding from the Application</value>
</data>
<data name="MissingWindowPrivateField" xml:space="preserve">
<value>CoreWindow is missing _window internal field required to access UIWindow, you may need an updated version of the Uno.Extension.Maui.WinUI package.</value>
</data>
<data name="NoColorValueProvided" xml:space="preserve">
<value>No value provided for the Maui Color.</value>
</data>
Expand Down