From fdc65ea2432776b00c239b28330ecf501266d582 Mon Sep 17 00:00:00 2001 From: Peter Spada Date: Wed, 7 May 2025 14:26:44 -0700 Subject: [PATCH] Add support for XAML Hot Reload in Visual Studio --- .../Platforms/Android/Impl/AndroidMopups.cs | 8 ++++---- .../Platforms/MacCatalyst/MacOSMopups.cs | 5 +++-- .../Platforms/Windows/Impl/PopupPlatformWindows.cs | 13 +++++-------- Mopups/Mopups.Maui/Platforms/iOS/iOSMopups.cs | 5 +++-- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/Mopups/Mopups.Maui/Platforms/Android/Impl/AndroidMopups.cs b/Mopups/Mopups.Maui/Platforms/Android/Impl/AndroidMopups.cs index 51469b8..0b29c2a 100644 --- a/Mopups/Mopups.Maui/Platforms/Android/Impl/AndroidMopups.cs +++ b/Mopups/Mopups.Maui/Platforms/Android/Impl/AndroidMopups.cs @@ -41,8 +41,8 @@ public class AndroidMopups : IPopupPlatform { HandleAccessibility(true, page.DisableAndroidAccessibilityHandling, page); - page.Parent = MauiApplication.Current.Application.Windows[0].Content as Element; - page.Parent ??= MauiApplication.Current.Application.Windows[0].Content as Element; + var mainPage = (Element)MauiApplication.Current.Application.Windows[0].Content; + mainPage.AddLogicalChild(page); var handler = page.Handler ??= new PopupPageHandler(page.Parent.Handler.MauiContext); @@ -56,13 +56,13 @@ public class AndroidMopups : IPopupPlatform { var renderer = IPopupPlatform.GetOrCreateHandler(page); - if(renderer != null) + if (renderer != null) { HandleAccessibility(false, page.DisableAndroidAccessibilityHandling, page); DecoreView?.RemoveView(renderer.PlatformView as Android.Views.View); renderer.DisconnectHandler(); //?? no clue if works - page.Parent = null; + page.Parent?.RemoveLogicalChild(page); return PostAsync(DecoreView); } diff --git a/Mopups/Mopups.Maui/Platforms/MacCatalyst/MacOSMopups.cs b/Mopups/Mopups.Maui/Platforms/MacCatalyst/MacOSMopups.cs index c657902..85ae314 100644 --- a/Mopups/Mopups.Maui/Platforms/MacCatalyst/MacOSMopups.cs +++ b/Mopups/Mopups.Maui/Platforms/MacCatalyst/MacOSMopups.cs @@ -22,7 +22,8 @@ internal class MacOSMopups : IPopupPlatform public Task AddAsync(PopupPage page) { - page.Parent = Application.Current.MainPage; + var mainPage = Application.Current.MainPage; + mainPage.AddLogicalChild(page); page.DescendantRemoved += HandleChildRemoved; @@ -102,7 +103,7 @@ internal class MacOSMopups : IPopupPlatform if (handler != null && viewController != null && !viewController.IsBeingDismissed) { var window = viewController.View?.Window; - page.Parent = null; + page.Parent?.RemoveLogicalChild(page); if (window != null) { diff --git a/Mopups/Mopups.Maui/Platforms/Windows/Impl/PopupPlatformWindows.cs b/Mopups/Mopups.Maui/Platforms/Windows/Impl/PopupPlatformWindows.cs index 417edfb..03460ab 100644 --- a/Mopups/Mopups.Maui/Platforms/Windows/Impl/PopupPlatformWindows.cs +++ b/Mopups/Mopups.Maui/Platforms/Windows/Impl/PopupPlatformWindows.cs @@ -51,7 +51,8 @@ namespace Mopups.Windows.Implementation public async Task AddAsync(PopupPage page) { - page.Parent = Application.Current.MainPage; + var mainPage = Application.Current.MainPage; + mainPage.AddLogicalChild(page); var popup = new global::Microsoft.UI.Xaml.Controls.Primitives.Popup(); @@ -62,16 +63,12 @@ namespace Mopups.Windows.Implementation // Then you can use contructor resolution instead of singletons // But I figured we could do that in a later PR and just work on windows here - var renderer = (PopupPageRenderer)page.ToPlatform(Application.Current.MainPage.Handler.MauiContext); - + var renderer = (PopupPageRenderer)page.ToPlatform(mainPage.Handler.MauiContext); renderer.Prepare(popup); popup.Child = renderer; - // https://github.com/microsoft/microsoft-ui-xaml/issues/3389 - popup.XamlRoot = - Application.Current.MainPage.Handler.MauiContext.Services.GetService().Content.XamlRoot; - + popup.XamlRoot = mainPage.Handler.MauiContext.Services.GetService().Content.XamlRoot; popup.IsOpen = true; page.ForceLayout(); @@ -91,7 +88,7 @@ namespace Mopups.Windows.Implementation renderer.Destroy(); Cleanup(page); - page.Parent = null; + page.Parent?.RemoveLogicalChild(page); popup.Child = null; popup.IsOpen = false; } diff --git a/Mopups/Mopups.Maui/Platforms/iOS/iOSMopups.cs b/Mopups/Mopups.Maui/Platforms/iOS/iOSMopups.cs index 3b1e48e..a01955a 100644 --- a/Mopups/Mopups.Maui/Platforms/iOS/iOSMopups.cs +++ b/Mopups/Mopups.Maui/Platforms/iOS/iOSMopups.cs @@ -16,7 +16,8 @@ internal class iOSMopups : IPopupPlatform public Task AddAsync(PopupPage page) { - page.Parent ??= Application.Current?.MainPage; + var mainPage = Application.Current.MainPage; + mainPage.AddLogicalChild(page); var keyWindow = GetKeyWindow(UIApplication.SharedApplication); if (keyWindow?.WindowLevel == UIWindowLevel.Normal) @@ -87,7 +88,7 @@ internal class iOSMopups : IPopupPlatform if (handler != null && viewController != null && !viewController.IsBeingDismissed) { var window = viewController.View?.Window; - page.Parent = null; + page.Parent?.RemoveLogicalChild(page); if (window != null) {