Skip to content

Commit

Permalink
chore: Fixing reference to UIWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
nickrandolph committed Apr 11, 2024
1 parent 454b9d4 commit a106876
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 40 deletions.
10 changes: 5 additions & 5 deletions samples/MauiEmbedding/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
<PackageVersion Include="Uno.Toolkit.WinUI" Version="5.0.15" />
<PackageVersion Include="Uno.Toolkit.WinUI.Material" Version="5.0.15" />
<PackageVersion Include="Uno.Resizetizer" Version="1.2.0" />
<PackageVersion Include="Uno.UI.Adapter.Microsoft.Extensions.Logging" Version="5.0.19" />
<PackageVersion Include="Uno.UI.Adapter.Microsoft.Extensions.Logging" Version="5.2.22" />
<PackageVersion Include="Uno.UniversalImageLoader" Version="1.9.36" />
<PackageVersion Include="Uno.WinUI" Version="5.0.19" />
<PackageVersion Include="Uno.WinUI.Skia.Gtk" Version="5.0.19" />
<PackageVersion Include="Uno.WinUI.RemoteControl" Version="5.0.19" />
<PackageVersion Include="Uno.WinUI" Version="5.2.22" />
<PackageVersion Include="Uno.WinUI.Skia.Gtk" Version="5.2.22" />
<PackageVersion Include="Uno.WinUI.RemoteControl" Version="5.2.22" />
<PackageVersion Include="CommunityToolkit.Maui" Version="2.0.0" />
<PackageVersion Include="Telerik.UI.for.Maui" Version="5.1.0" Condition="$(_UseTelerik)" />
<PackageVersion Include="Esri.ArcGISRuntime.Maui" Version="200.1.0" />
<PackageVersion Include="Microsoft.Windows.Compatibility" Version="7.0.4" />
<PackageVersion Include="Uno.Extensions.Logging.WebAssembly.Console" Version="1.7.0" />
<PackageVersion Include="Uno.WinUI.WebAssembly" Version="5.0.19" />
<PackageVersion Include="Uno.WinUI.WebAssembly" Version="5.2.22" />
<PackageVersion Include="Uno.Wasm.Bootstrap" Version="8.0.7" />
<PackageVersion Include="Uno.Wasm.Bootstrap.DevServer" Version="8.0.7" />
</ItemGroup>
Expand Down
75 changes: 40 additions & 35 deletions src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs
Original file line number Diff line number Diff line change
@@ -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<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>());
// 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<UIWindow>(sp =>
{
var window = sp.GetRequiredService<Microsoft.UI.Xaml.Window>();

// 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<IUIApplicationDelegate>(sp => sp.GetRequiredService<Application>());

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<IApplication>();
var iApp = mauiApp.Services.GetRequiredService<IApplication>();

Microsoft.Maui.ApplicationModel.Platform.Init(() => mauiApp.Services.GetRequiredService<UIWindow>().RootViewController!);
_ = new EmbeddedApplication(mauiApp.Services, iApp);
app.SetApplicationHandler(iApp, rootContext);
InitializeApplicationMainPage(iApp);
}
Microsoft.Maui.ApplicationModel.Platform.Init(() => mauiApp.Services.GetRequiredService<UIWindow>().RootViewController!);
_ = new EmbeddedApplication(mauiApp.Services, iApp);
app.SetApplicationHandler(iApp, rootContext);
InitializeApplicationMainPage(iApp);
}
}

0 comments on commit a106876

Please sign in to comment.