Skip to content

Commit

Permalink
Merge pull request #1832 from unoplatform/dev/nr/imageconverter
Browse files Browse the repository at this point in the history
fix: Adding UIWindow to the service container
  • Loading branch information
nickrandolph authored Aug 29, 2023
2 parents 96d48ed + 77b1b12 commit 61cbe29
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
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

0 comments on commit 61cbe29

Please sign in to comment.