From a1916da5435827b47765740e7dd31be5ee4b2a41 Mon Sep 17 00:00:00 2001 From: Nick Randolph Date: Fri, 12 Apr 2024 00:17:02 +1000 Subject: [PATCH 01/11] chore: Fixing reference to UIWindow (cherry picked from commit a106876453a8b0821fbf10e7ed528f302d588fe4) --- .../MauiEmbedding/Directory.Packages.props | 10 +-- .../MauiEmbedding.apple.cs | 75 ++++++++++--------- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/samples/MauiEmbedding/Directory.Packages.props b/samples/MauiEmbedding/Directory.Packages.props index 7c2df978ee..d786f5db05 100644 --- a/samples/MauiEmbedding/Directory.Packages.props +++ b/samples/MauiEmbedding/Directory.Packages.props @@ -15,17 +15,17 @@ - + - - - + + + - + diff --git a/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs b/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs index 260526e2e8..7e5632e9bd 100644 --- a/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs +++ b/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs @@ -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(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()); + // 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(sp => + { + var window = sp.GetRequiredService(); + + // 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(sp => sp.GetRequiredService()); - 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(); + var iApp = mauiApp.Services.GetRequiredService(); - Microsoft.Maui.ApplicationModel.Platform.Init(() => mauiApp.Services.GetRequiredService().RootViewController!); - _ = new EmbeddedApplication(mauiApp.Services, iApp); - app.SetApplicationHandler(iApp, rootContext); - InitializeApplicationMainPage(iApp); - } + Microsoft.Maui.ApplicationModel.Platform.Init(() => mauiApp.Services.GetRequiredService().RootViewController!); + _ = new EmbeddedApplication(mauiApp.Services, iApp); + app.SetApplicationHandler(iApp, rootContext); + InitializeApplicationMainPage(iApp); + } } From 76a1ecbaf4574ad3cb61071f4504e991d4fdc3c3 Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Thu, 11 Apr 2024 12:18:43 -0600 Subject: [PATCH 02/11] chore: use 5.2 for MAUI Embedding (cherry picked from commit 72b698f29ead198218144e647c7f37ec9a813eaf) # Conflicts: # src/Directory.Packages.props --- src/Directory.Packages.props | 6 ++++ .../MauiEmbedding.apple.cs | 29 ++++--------------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index 2038590fbc..c04cc7b7a3 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -53,8 +53,14 @@ +<<<<<<< HEAD +======= + + + +>>>>>>> 72b698f29 (chore: use 5.2 for MAUI Embedding) diff --git a/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs b/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs index 7e5632e9bd..67bafc355b 100644 --- a/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs +++ b/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs @@ -6,29 +6,12 @@ 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.AddSingleton(sp => - { - var window = sp.GetRequiredService(); - - // 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!; - }) + builder.Services.AddTransient(sp => + { + var window = sp.GetRequiredService(); + var nativeWindow = WindowHelper.GetNativeWindow(window); + return nativeWindow is UIWindow uiWindow ? uiWindow : throw new InvalidOperationException("Unable to locate the Native UIWindow"); + }) .AddSingleton(sp => sp.GetRequiredService()); return builder; From 2ef0b4151a1241ae61388ef5d885573b38393d2c Mon Sep 17 00:00:00 2001 From: Nick Randolph Date: Tue, 16 Apr 2024 01:44:25 +1000 Subject: [PATCH 03/11] chore: Restoring namespaces (cherry picked from commit 42d5a706bb1a18a4ee65ff1172a10f5a3d2669d1) --- src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs b/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs index 67bafc355b..57a45412b8 100644 --- a/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs +++ b/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs @@ -1,3 +1,8 @@ +using UIKit; +using Uno.Extensions.Maui.Platform; +using Microsoft.Maui; +using Windows.UI.Core; + namespace Uno.Extensions.Maui; partial class MauiEmbedding From 27d81d2f19694a14ddc394a044a1e5f3a470b159 Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Mon, 15 Apr 2024 11:25:01 -0600 Subject: [PATCH 04/11] chore: adding missing namespace (cherry picked from commit e89d6050b5ac66ce4f7cf4f66f426ddc1aeff3b4) --- src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs b/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs index 57a45412b8..1edbbf42be 100644 --- a/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs +++ b/src/Uno.Extensions.Maui.UI/MauiEmbedding.apple.cs @@ -2,6 +2,7 @@ using Uno.Extensions.Maui.Platform; using Microsoft.Maui; using Windows.UI.Core; +using Uno.UI.Xaml; namespace Uno.Extensions.Maui; From dfdb532345091e347c802d38d40c2c51671876bb Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Tue, 16 Apr 2024 17:15:11 -0600 Subject: [PATCH 05/11] fix: use Uno 5.2 for MauiEmbedding Sample (cherry picked from commit c19353546f94b7c55ea3c8af2e70381ddf63d99d) # Conflicts: # src/Directory.Packages.props --- samples/MauiEmbedding/Directory.Build.props | 2 ++ .../MauiEmbedding/Directory.Packages.props | 27 +++++++++--------- .../MauiEmbedding.MauiControls.csproj | 6 ++-- .../MauiEmbedding.Mobile.csproj | 3 +- src/Directory.Packages.props | 28 +++++++++++++++---- .../ApplicationBuilder.cs | 4 ++- .../Navigators/ContentDialogNavigator.cs | 2 +- .../ServiceProviderExtensions.cs | 3 +- src/maui-embedding.props | 6 ++++ 9 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 src/maui-embedding.props diff --git a/samples/MauiEmbedding/Directory.Build.props b/samples/MauiEmbedding/Directory.Build.props index 9428bd358b..0915308f4f 100644 --- a/samples/MauiEmbedding/Directory.Build.props +++ b/samples/MauiEmbedding/Directory.Build.props @@ -1,4 +1,6 @@ + + enable enable diff --git a/samples/MauiEmbedding/Directory.Packages.props b/samples/MauiEmbedding/Directory.Packages.props index d786f5db05..2834fb8e51 100644 --- a/samples/MauiEmbedding/Directory.Packages.props +++ b/samples/MauiEmbedding/Directory.Packages.props @@ -1,13 +1,13 @@ - + - - - - - + + + + + @@ -15,18 +15,17 @@ - + - - - - + + + - - - + + + diff --git a/samples/MauiEmbedding/MauiEmbedding.MauiControls/MauiEmbedding.MauiControls.csproj b/samples/MauiEmbedding/MauiEmbedding.MauiControls/MauiEmbedding.MauiControls.csproj index 23065f29cc..93319bf3e4 100644 --- a/samples/MauiEmbedding/MauiEmbedding.MauiControls/MauiEmbedding.MauiControls.csproj +++ b/samples/MauiEmbedding/MauiEmbedding.MauiControls/MauiEmbedding.MauiControls.csproj @@ -1,4 +1,4 @@ - + net8.0;net8.0-android;net8.0-ios;net8.0-maccatalyst @@ -16,8 +16,8 @@ - - + + diff --git a/samples/MauiEmbedding/MauiEmbedding.Mobile/MauiEmbedding.Mobile.csproj b/samples/MauiEmbedding/MauiEmbedding.Mobile/MauiEmbedding.Mobile.csproj index d9314982e3..95421c1e30 100644 --- a/samples/MauiEmbedding/MauiEmbedding.Mobile/MauiEmbedding.Mobile.csproj +++ b/samples/MauiEmbedding/MauiEmbedding.Mobile/MauiEmbedding.Mobile.csproj @@ -1,4 +1,4 @@ - + @@ -37,7 +37,6 @@ - diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index c04cc7b7a3..6b8597e49e 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -1,4 +1,15 @@ + + + 5.0.19 + 5.2.22 + 1.4.231219000 + + + @@ -23,7 +34,7 @@ - + @@ -48,11 +59,12 @@ - - - - + + + + +<<<<<<< HEAD <<<<<<< HEAD @@ -63,6 +75,12 @@ >>>>>>> 72b698f29 (chore: use 5.2 for MAUI Embedding) +======= + + + + +>>>>>>> c19353546 (fix: use Uno 5.2 for MauiEmbedding Sample) diff --git a/src/Uno.Extensions.Hosting.UI/ApplicationBuilder.cs b/src/Uno.Extensions.Hosting.UI/ApplicationBuilder.cs index 19facf6269..35e96164aa 100644 --- a/src/Uno.Extensions.Hosting.UI/ApplicationBuilder.cs +++ b/src/Uno.Extensions.Hosting.UI/ApplicationBuilder.cs @@ -10,7 +10,9 @@ internal record ApplicationBuilder(Application App, LaunchActivatedEventArgs Arg #if NET6_0_OR_GREATER && WINDOWS && !HAS_UNO new Window(); #else - Window.Current; + // Window.Current can be null with Uno.WinUI 5.2+ + // When updating Hosting to Uno.WinUI 5.2+ we need to remove the compiler directive + Window.Current ?? new Window(); #endif public IHost Build() diff --git a/src/Uno.Extensions.Navigation.UI/Navigators/ContentDialogNavigator.cs b/src/Uno.Extensions.Navigation.UI/Navigators/ContentDialogNavigator.cs index 67a65d256d..1b6892274c 100644 --- a/src/Uno.Extensions.Navigation.UI/Navigators/ContentDialogNavigator.cs +++ b/src/Uno.Extensions.Navigation.UI/Navigators/ContentDialogNavigator.cs @@ -51,7 +51,7 @@ services is null || } #if WINUI - dialog.XamlRoot = Window!.Content.XamlRoot; + dialog.XamlRoot = Window!.Content!.XamlRoot; #endif dialog.SetInstance(Region); diff --git a/src/Uno.Extensions.Navigation.UI/ServiceProviderExtensions.cs b/src/Uno.Extensions.Navigation.UI/ServiceProviderExtensions.cs index 064bf6b507..0e739d9efc 100644 --- a/src/Uno.Extensions.Navigation.UI/ServiceProviderExtensions.cs +++ b/src/Uno.Extensions.Navigation.UI/ServiceProviderExtensions.cs @@ -10,7 +10,8 @@ public static class ServiceProviderExtensions /// The IServiceProvider instance to attach public async static Task AttachServicesAsync(this Window window, IServiceProvider services) { - return await window.Content + // NOTE: This creates a nullable error in Uno.WinUI 5.2+ + return await window.Content! .AttachServiceProvider(services) .RegisterWindowAsync(window); } diff --git a/src/maui-embedding.props b/src/maui-embedding.props new file mode 100644 index 0000000000..1ffd11f373 --- /dev/null +++ b/src/maui-embedding.props @@ -0,0 +1,6 @@ + + + 5.2.22 + 1.5.240404000 + + From fc7c212ee3863b3b33a14498c747dfbbe10b210b Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Tue, 16 Apr 2024 18:03:16 -0600 Subject: [PATCH 06/11] chore: fix for UWP (cherry picked from commit e203ffde6c76759e4151e35db1231bd1515d519c) --- src/Uno.Extensions.Hosting.UI/ApplicationBuilder.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Uno.Extensions.Hosting.UI/ApplicationBuilder.cs b/src/Uno.Extensions.Hosting.UI/ApplicationBuilder.cs index 35e96164aa..92d0dbc5b4 100644 --- a/src/Uno.Extensions.Hosting.UI/ApplicationBuilder.cs +++ b/src/Uno.Extensions.Hosting.UI/ApplicationBuilder.cs @@ -1,4 +1,4 @@ -namespace Uno.Extensions.Hosting; +namespace Uno.Extensions.Hosting; internal record ApplicationBuilder(Application App, LaunchActivatedEventArgs Arguments, Assembly ApplicationAssembly) : IApplicationBuilder { @@ -9,10 +9,12 @@ internal record ApplicationBuilder(Application App, LaunchActivatedEventArgs Arg public Window Window { get; } = #if NET6_0_OR_GREATER && WINDOWS && !HAS_UNO new Window(); -#else +#elif HAS_UNO_WINUI // Window.Current can be null with Uno.WinUI 5.2+ // When updating Hosting to Uno.WinUI 5.2+ we need to remove the compiler directive Window.Current ?? new Window(); +#else + Window.Current; #endif public IHost Build() From db028a29ee123d1daed1d7594ffbbaf927da6ba9 Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Wed, 17 Apr 2024 07:44:38 -0600 Subject: [PATCH 07/11] chore: revert Maui version (cherry picked from commit 034d966f6642723df72f5e6f2a68ef4732649548) --- .../MauiEmbedding.MauiControls.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/MauiEmbedding/MauiEmbedding.MauiControls/MauiEmbedding.MauiControls.csproj b/samples/MauiEmbedding/MauiEmbedding.MauiControls/MauiEmbedding.MauiControls.csproj index 93319bf3e4..4f0f0f3974 100644 --- a/samples/MauiEmbedding/MauiEmbedding.MauiControls/MauiEmbedding.MauiControls.csproj +++ b/samples/MauiEmbedding/MauiEmbedding.MauiControls/MauiEmbedding.MauiControls.csproj @@ -16,8 +16,8 @@ - - + + From f5c3de1d2edb409c8a37564bd7e34d82e10a626c Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Wed, 17 Apr 2024 07:44:53 -0600 Subject: [PATCH 08/11] chore: add condition for handler on MacCatalyst (cherry picked from commit a93c322fdf2f524ccc021cb2ac4a6be9b5ae4de3) --- samples/MauiEmbedding/MauiEmbedding.MauiControls/CustomEntry.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/MauiEmbedding/MauiEmbedding.MauiControls/CustomEntry.cs b/samples/MauiEmbedding/MauiEmbedding.MauiControls/CustomEntry.cs index f8047d4f8d..b27c974e95 100644 --- a/samples/MauiEmbedding/MauiEmbedding.MauiControls/CustomEntry.cs +++ b/samples/MauiEmbedding/MauiEmbedding.MauiControls/CustomEntry.cs @@ -24,7 +24,7 @@ internal static void Init() return; } -#if IOS +#if IOS || MACCATALYST if (h.PlatformView is UIKit.UITextField textField) { var color = entry.BorderColor?.ToPlatform().CGColor ?? UIKit.UIColor.Black.CGColor; From eefddbf0032abf4875fb1a7801829a13faebef0d Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Wed, 17 Apr 2024 08:45:44 -0600 Subject: [PATCH 09/11] chore: add note for ! operator (cherry picked from commit 12e6e6cc879c869cc63d8d9eafac221cf8fe4182) --- .../Navigators/ContentDialogNavigator.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Uno.Extensions.Navigation.UI/Navigators/ContentDialogNavigator.cs b/src/Uno.Extensions.Navigation.UI/Navigators/ContentDialogNavigator.cs index 1b6892274c..8c3878c2b6 100644 --- a/src/Uno.Extensions.Navigation.UI/Navigators/ContentDialogNavigator.cs +++ b/src/Uno.Extensions.Navigation.UI/Navigators/ContentDialogNavigator.cs @@ -51,6 +51,7 @@ services is null || } #if WINUI + // NOTE: We needed to add the ! operator on Content due to a nullability issue starting in Uno.WinUI 5.2 dialog.XamlRoot = Window!.Content!.XamlRoot; #endif From ca01c96ba2949482e52fa7b228d49711d8a130b7 Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Wed, 17 Apr 2024 08:54:14 -0600 Subject: [PATCH 10/11] chore: bump MSAL to latest due to security vulnerability (cherry picked from commit 54908ce274702cdcdf36a768ff2ce17eeba63d56) --- src/Directory.Packages.props | 2 +- testing/TestHarness/Directory.Packages.props | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index 6b8597e49e..a75adc583a 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -25,7 +25,7 @@ - + diff --git a/testing/TestHarness/Directory.Packages.props b/testing/TestHarness/Directory.Packages.props index c58edf7d0c..2a54f1e22e 100644 --- a/testing/TestHarness/Directory.Packages.props +++ b/testing/TestHarness/Directory.Packages.props @@ -11,7 +11,7 @@ - + From 250c0b46d1c09661b19dcc9a46922552d62ff8b3 Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Wed, 17 Apr 2024 09:49:42 -0600 Subject: [PATCH 11/11] chore: resolving merge conflict --- src/Directory.Packages.props | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index a75adc583a..6f09e2b82e 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -64,23 +64,10 @@ -<<<<<<< HEAD -<<<<<<< HEAD - - -======= - - - ->>>>>>> 72b698f29 (chore: use 5.2 for MAUI Embedding) - - -======= - + ->>>>>>> c19353546 (fix: use Uno 5.2 for MauiEmbedding Sample)