From 0f87e408c53a8634af19fc643d0772eaae69e729 Mon Sep 17 00:00:00 2001 From: Nick Randolph Date: Thu, 31 Oct 2024 23:51:54 +1100 Subject: [PATCH] fix: Adjusting activation to occur once the content loads --- .../WindowExtensions.cs | 4 +-- .../ServiceProviderExtensions.cs | 10 ++++-- .../WindowExtensions.cs | 34 +++++++++++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 src/Uno.Extensions.Navigation.UI/WindowExtensions.cs diff --git a/src/Uno.Extensions.Navigation.Toolkit/WindowExtensions.cs b/src/Uno.Extensions.Navigation.Toolkit/WindowExtensions.cs index 273c2746ab..795a354edb 100644 --- a/src/Uno.Extensions.Navigation.Toolkit/WindowExtensions.cs +++ b/src/Uno.Extensions.Navigation.Toolkit/WindowExtensions.cs @@ -53,7 +53,7 @@ internal static void ApplyLoadingTask(this Window window, FrameworkElement root, loadingTask = new Func(async () => { await navInit; - window.Activate(); + window.ActivateWhenReady(); })(); } } @@ -64,7 +64,7 @@ internal static void ApplyLoadingTask(this Window window, FrameworkElement root, if (activate) { // Activate immediately to show the splash screen - window.Activate(); + window.ActivateWhenReady(); } } } diff --git a/src/Uno.Extensions.Navigation.UI/ServiceProviderExtensions.cs b/src/Uno.Extensions.Navigation.UI/ServiceProviderExtensions.cs index 459246f896..cb4050a65a 100644 --- a/src/Uno.Extensions.Navigation.UI/ServiceProviderExtensions.cs +++ b/src/Uno.Extensions.Navigation.UI/ServiceProviderExtensions.cs @@ -1,5 +1,8 @@ namespace Uno.Extensions; +/// +/// Extension methods on . +/// public static class ServiceProviderExtensions { /// @@ -46,7 +49,7 @@ internal static IServiceProvider CreateNavigationScope(this IServiceProvider ser .CloneScopedInstance(services); } - + /// /// Initializes navigation for an application using a ContentControl @@ -122,11 +125,12 @@ private static async Task BuildAndInitializeHostAsync( await Task.Run(() => host.StartAsync()); - // Fallback to make sure the window is activated - window.Activate(); + window.ActivateWhenReady(); await startup; return host; } + + private static void Content_Loaded(object sender, RoutedEventArgs e) => throw new NotImplementedException(); } diff --git a/src/Uno.Extensions.Navigation.UI/WindowExtensions.cs b/src/Uno.Extensions.Navigation.UI/WindowExtensions.cs new file mode 100644 index 0000000000..49e6d279c3 --- /dev/null +++ b/src/Uno.Extensions.Navigation.UI/WindowExtensions.cs @@ -0,0 +1,34 @@ +namespace Uno.Extensions; + +/// +/// Extension methods on . +/// +public static class WindowExtensions +{ + /// + /// Activates the window when the content is ready + /// + /// The to activate + public static void ActivateWhenReady(this Window window) + { + + if (window.Content is FrameworkElement content) + { + if (content.IsLoaded) + { + // Fallback to make sure the window is activated + window.Activate(); + } + else + { + content.Loaded += (_, _) => window.Activate(); + content.SizeChanged += (_, _) => window.Activate(); + content.Loading += (_, _) => window.Activate(); + } + } + else + { + window.Activate(); + } + } +}