From 591bc9cf9195492e33df58c1da34947212ac4e1e Mon Sep 17 00:00:00 2001 From: Mike Corsaro Date: Fri, 8 Nov 2024 11:23:58 -0800 Subject: [PATCH] Add logic to clear content ASAP if unloaded event didn't yet fire --- .../src/Platform/Windows/StackNavigationManager.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Core/src/Platform/Windows/StackNavigationManager.cs b/src/Core/src/Platform/Windows/StackNavigationManager.cs index 252a49bd19..2901c31c7d 100644 --- a/src/Core/src/Platform/Windows/StackNavigationManager.cs +++ b/src/Core/src/Platform/Windows/StackNavigationManager.cs @@ -14,6 +14,7 @@ namespace Microsoft.Maui.Platform IMauiContext _mauiContext; Frame? _navigationFrame; Action? _pendingNavigationFinished; + ContentControl? _previousContent; bool _connected; protected NavigationRootManager WindowManager => _mauiContext.GetNavigationRootManager(); @@ -162,8 +163,15 @@ namespace Microsoft.Maui.Platform HorizontalAlignment = UI.Xaml.HorizontalAlignment.Stretch, VerticalAlignment = UI.Xaml.VerticalAlignment.Stretch }; - presenter.Unloaded += Presenter_Unloaded; + // It's possible that the unloaded event didn't happen yet, so force clear the content + if (_previousContent is not null) + { + _previousContent.Content = null; + _previousContent = null; + } + + presenter.Unloaded += Presenter_Unloaded; page.Content = presenter; } else @@ -190,6 +198,8 @@ namespace Microsoft.Maui.Platform _pendingNavigationFinished = () => { + _previousContent = presenter; + if (presenter?.Content is not FrameworkElement pc) { FireNavigationFinished();