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

chore: Fixing reference to UIWindow #2246

Merged
merged 10 commits into from
Apr 17, 2024
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);
}
}
Loading