Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ snapshots-diff/
#install of dotnet version
/.dotnet
.dotnet
.mono/
temp
.packages
/src/Templates/.tempTemplateOutput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,87 @@ await CreateHandlerAndAddToWindow<WindowHandlerStub>(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<WindowHandlerStub>(window, async handler =>
{
await OnLoadedAsync(content);
var shellView = Assert.IsAssignableFrom<RootNavigationView>(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<IWindowHandler>(new ContentPage(), handler =>
{
var manager = new NavigationRootManager(handler.PlatformView);
var content = new Microsoft.UI.Xaml.Controls.Border();
var rootView = Assert.IsType<WindowRootView>(manager.RootView);

try
{
manager.Connect(useExistingNavigationView ? new RootNavigationView { Content = content } : content);
var navigationView = Assert.IsType<RootNavigationView>(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.
Expand Down
15 changes: 12 additions & 3 deletions src/Core/src/Platform/Windows/NavigationRootManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public partial class NavigationRootManager
{
readonly WeakReference<Window> _platformWindow;
WindowRootView _rootView;
RootNavigationView? _navigationView;
bool _disconnected = true;
internal event EventHandler? OnApplyTemplateFinished;

Expand Down Expand Up @@ -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))
{
Expand All @@ -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;
Expand Down
Loading