diff --git a/.gitignore b/.gitignore index f747463d5017..894153192e78 100644 --- a/.gitignore +++ b/.gitignore @@ -381,6 +381,7 @@ snapshots-diff/ #install of dotnet version /.dotnet .dotnet +.mono/ temp .packages /src/Templates/.tempTemplateOutput diff --git a/src/Controls/tests/DeviceTests/Elements/Window/WindowTests.Windows.cs b/src/Controls/tests/DeviceTests/Elements/Window/WindowTests.Windows.cs index 1d04aa5d5f78..c6d7e2fc64e9 100644 --- a/src/Controls/tests/DeviceTests/Elements/Window/WindowTests.Windows.cs +++ b/src/Controls/tests/DeviceTests/Elements/Window/WindowTests.Windows.cs @@ -383,6 +383,87 @@ await CreateHandlerAndAddToWindow(window, async (handler) => }); } + [Fact] + public async Task RestoringSameShellPreservesContentAndTitleBar() + { + SetupBuilder(); + + var content = new Label { Text = "Issue 38619" }; + var shellPage = new ContentPage { Content = content }; + var shell = new Shell { CurrentItem = shellPage }; + var titleBar = new TitleBar { Title = "Retained Shell" }; + var window = new Window(shell) { TitleBar = titleBar }; + + await CreateHandlerAndAddToWindow(window, async handler => + { + await OnLoadedAsync(content); + var shellView = Assert.IsAssignableFrom(shell.Handler.PlatformView); + var shellContent = shellView.Content; + Assert.NotNull(shellContent); + + for (int i = 0; i < 2; i++) + { + var replacement = new ContentPage { Content = new Label { Text = "Replacement" } }; + window.Page = replacement; + await OnLoadedAsync(replacement); + await OnUnloadedAsync(content); + + window.Page = shell; + + // Disconnect must not clear the handler-owned ShellView's content. + Assert.Same(shellView, shell.Handler.PlatformView); + Assert.Same(shellContent, shellView.Content); + await OnLoadedAsync(shellPage); + await OnLoadedAsync(content); + + var rootView = GetWindowRootView(handler); + var platformContent = content.ToPlatform(handler.MauiContext); + Assert.Same(shellView, rootView.Content); + Assert.True(platformContent.IsLoaded); + Assert.Same(rootView.XamlRoot, platformContent.XamlRoot); + await AssertEventually(() => platformContent.ActualWidth > 0 && platformContent.ActualHeight > 0); + + Assert.Same(titleBar, rootView.TitleBar); + await OnLoadedAsync(titleBar); + Assert.True(titleBar.ToPlatform(handler.MauiContext).IsLoaded); + } + }); + } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public async Task DisconnectOnlyClearsManagerCreatedNavigationView(bool useExistingNavigationView) + { + SetupBuilder(); + + await CreateHandlerAndAddToWindow(new ContentPage(), handler => + { + var manager = new NavigationRootManager(handler.PlatformView); + var content = new Microsoft.UI.Xaml.Controls.Border(); + var rootView = Assert.IsType(manager.RootView); + + try + { + manager.Connect(useExistingNavigationView ? new RootNavigationView { Content = content } : content); + var navigationView = Assert.IsType(rootView.Content); + Assert.Same(content, navigationView.Content); + + manager.Disconnect(); + + Assert.Null(rootView.Content); + if (useExistingNavigationView) + Assert.Same(content, navigationView.Content); + else + Assert.Null(navigationView.Content); + } + finally + { + manager.Disconnect(); + } + }); + } + // MinimizeAndThenMaximizingWorks test moved to UI tests (Issue14142) because // window activation events don't fire reliably on Helix VMs which run in // non-interactive Windows sessions. diff --git a/src/Core/src/Platform/Windows/NavigationRootManager.cs b/src/Core/src/Platform/Windows/NavigationRootManager.cs index 62a8aa53eecf..a974a65c147b 100644 --- a/src/Core/src/Platform/Windows/NavigationRootManager.cs +++ b/src/Core/src/Platform/Windows/NavigationRootManager.cs @@ -9,6 +9,7 @@ public partial class NavigationRootManager { readonly WeakReference _platformWindow; WindowRootView _rootView; + RootNavigationView? _navigationView; bool _disconnected = true; internal event EventHandler? OnApplyTemplateFinished; @@ -122,10 +123,14 @@ public virtual void Connect(UIElement? platformView) _rootView.Content = null; } - _rootView.Content = platformView is NavigationView ? platformView : new RootNavigationView() + if (_navigationView is not null) + _navigationView.Content = null; + + _navigationView = platformView is NavigationView ? null : new RootNavigationView() { Content = platformView }; + _rootView.Content = _navigationView ?? platformView; if (_disconnected && _platformWindow.TryGetTarget(out var platformWindow)) { @@ -149,8 +154,12 @@ public virtual void Disconnect() _rootView.AppWindowId = null; - if (_rootView.Content is RootNavigationView navView) - navView.Content = null; + // Only clear the wrapper we created, not a page-owned navigation view such as Shell. + if (_navigationView is not null) + { + _navigationView.Content = null; + _navigationView = null; + } _rootView.Content = null; _disconnected = true;