Skip to content

Commit

Permalink
fix: Ensure root visual roots are removed when content changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Jan 19, 2024
1 parent 3076e7b commit 0a70ba9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Hosting/DesktopWindowXamlSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void Dispose()

internal void AttachToWindow(Window window)
{
ContentManager.AttachToWindow(Content, window);
ContentManager.AttachToWindow(XamlIsland, window);
_xamlIsland.OwnerWindow = window;
}
}
23 changes: 15 additions & 8 deletions src/Uno.UI/UI/Xaml/Internal/ContentManager/ContentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,25 @@ private void SetContent(UIElement? newContent)
}
// TODO: Add RootScrollViewer everywhere
visualTree.SetPublicRootVisual(newContent, rootScrollViewer: null, rootContentPresenter: null);
_rootVisual = WinUICoreServices.Instance.MainRootVisual;

if (_rootVisual?.XamlRoot is null)
if (_rootVisual is null)
{
throw new InvalidOperationException("The root visual was not created.");
}
// Initialize root visual and attach it to owner window.

if (_owner is not Windows.UI.Xaml.Window window)
{
throw new InvalidOperationException("Owner of ContentManager should be a Window");
_rootVisual = WinUICoreServices.Instance.MainRootVisual;

if (_rootVisual?.XamlRoot is null)
{
throw new InvalidOperationException("The root visual was not created.");
}

if (_owner is not Windows.UI.Xaml.Window window)
{
throw new InvalidOperationException("Owner of ContentManager should be a Window");
}

AttachToWindow(_rootVisual, window);
}
AttachToWindow(_rootVisual, window);
}

_content = newContent;
Expand Down
18 changes: 14 additions & 4 deletions src/Uno.UI/UI/Xaml/Internal/VisualTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,16 @@ private void ResetRoots()
// }
//}

if (FocusVisualRoot is not null)
{
RemoveRoot(FocusVisualRoot);
}

if (PopupRoot is not null)
{
RemoveRoot(PopupRoot);
}

//if (_printRoot != null)
//{
// RemoveRoot(_printRoot);
Expand All @@ -456,10 +466,10 @@ private void ResetRoots()
// RemoveRoot(_transitionRoot);
//}

//if (_fullWindowMediaRoot != null)
//{
// RemoveRoot(_fullWindowMediaRoot);
//}
if (FullWindowMediaRoot != null)
{
RemoveRoot(FullWindowMediaRoot);
}

//if (_renderTargetBitmapRoot != null)
//{
Expand Down

0 comments on commit 0a70ba9

Please sign in to comment.