Skip to content
Merged
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
53 changes: 34 additions & 19 deletions src/Controls/src/Core/Handlers/Shell/Windows/ShellFlyoutItemView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public partial class ShellFlyoutItemView : ContentControl
object _previousDataContext;
Shell _shell;
bool _updatingVisualState;
ShellView ShellView => _shell.Handler?.PlatformView as ShellView;
ShellView ShellView => _shell?.Handler?.PlatformView as ShellView;

public ShellFlyoutItemView()
{
Expand All @@ -39,24 +39,7 @@ void OnDataContextChanged(Microsoft.UI.Xaml.FrameworkElement sender, Microsoft.U
return;

_previousDataContext = args.NewValue;
if (_content != null)
{
if (_content.BindingContext is INotifyPropertyChanged inpc)
inpc.PropertyChanged -= ShellElementPropertyChanged;

if (_content.Parent is BaseShellItem bsi)
bsi.RemoveLogicalChild(_content);
else
_shell?.RemoveLogicalChild(_content);

// Remove resource listener from previous content
if (_content is IElementDefinition contentDef)
contentDef.RemoveResourcesChangedListener(OnResourcesChanged);
_content.Cleanup();
_content.BindingContext = null;
_content.Parent = null;
_content = null;
}
ReleaseContent();

BindableObject bo = null;

Expand Down Expand Up @@ -100,6 +83,38 @@ void OnDataContextChanged(Microsoft.UI.Xaml.FrameworkElement sender, Microsoft.U
}
}

// Called when the shell's handler disconnects. The DataContext does not change then, and WinUI keeps
// this view alive past that, so without it the view would keep the shell and its pages alive too.
internal void Disconnect()
{
ReleaseContent();
Content = null;
_shell = null;
Comment thread
FullDevStudio marked this conversation as resolved.
Resolved
Comment thread
FullDevStudio marked this conversation as resolved.
Resolved
_previousDataContext = null;
}

void ReleaseContent()
{
if (_content == null)
return;

if (_content.BindingContext is INotifyPropertyChanged inpc)
inpc.PropertyChanged -= ShellElementPropertyChanged;

if (_content.Parent is BaseShellItem bsi)
bsi.RemoveLogicalChild(_content);
else
_shell?.RemoveLogicalChild(_content);

// Remove resource listener from previous content
if (_content is IElementDefinition contentDef)
contentDef.RemoveResourcesChangedListener(OnResourcesChanged);
_content.Cleanup();
_content.BindingContext = null;
_content.Parent = null;
_content = null;
}

void ShellElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.Is(BaseShellItem.IsCheckedProperty))
Expand Down
5 changes: 5 additions & 0 deletions src/Controls/src/Core/Handlers/Shell/Windows/ShellView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ internal void Disconnect()
element.Handler?.DisconnectHandler();
}

foreach (var flyoutItem in this.GetChildren<ShellFlyoutItemView>())
{
flyoutItem?.Disconnect();
}

ItemRenderer = null;
Content = null;
Element = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,76 @@ await AssertEventually(() => searchBox.IsLoaded &&
});
}

[Fact(DisplayName = "Flyout Items Release The Shell When It Is Swapped")]
public async Task FlyoutItemsReleaseTheShellWhenItIsSwapped()
{
SetupBuilder();

var window = new Controls.Window(new ContentPage());
await CreateHandlerAndAddToWindow<WindowHandlerStub>(window, async handler =>
{
await OnLoadedAsync(window.Page);

var shellReference = await ShowShellAndSwapItOut(window);

// The window is still open here, so this checks the swap and not the window teardown.
// With the window open the old shell only becomes collectable after a few dispatcher
// passes, so collect with a pause in between rather than in a tight loop.
await AssertEventually(() =>
{
GC.Collect();
GC.WaitForPendingFinalizers();
return !shellReference.IsAlive;
}, timeout: 10000, interval: 250, message: "The old shell was not collected");
}, timeOut: TimeSpan.FromSeconds(30));
}

// Not inlined so no local of the old shell ends up in the test's state machine.
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
async Task<WeakReference> ShowShellAndSwapItOut(Controls.Window window)
{
var shell = await CreateShellAsync(AddFlyoutItems);
window.Page = shell;
await OnLoadedAsync(shell.CurrentPage);

var shellView = (ShellView)shell.Handler.PlatformView;
await AssertEventually(() => shellView.GetChildren<ShellFlyoutItemView>().Any());

var shellHandler = shell.Handler;
var newShell = await CreateShellAsync(AddFlyoutItems);
window.Page = newShell;
await OnLoadedAsync(newShell.CurrentPage);
shellHandler.DisconnectHandler();

return new WeakReference(shell);
}

[Fact(DisplayName = "Flyout Item Can Be Measured After The Shell Disconnects")]
public async Task FlyoutItemCanBeMeasuredAfterTheShellDisconnects()
{
SetupBuilder();

var shell = await CreateShellAsync(AddFlyoutItems);
await CreateHandlerAndAddToWindow<ShellHandler>(shell, async handler =>
{
var shellView = handler.PlatformView;
await AssertEventually(() => shellView.GetChildren<ShellFlyoutItemView>().Any());
var flyoutItem = shellView.GetChildren<ShellFlyoutItemView>().First();

((IElementHandler)handler).DisconnectHandler();

flyoutItem.InvalidateMeasure();
flyoutItem.Measure(new global::Windows.Foundation.Size(200, 200));
});
}

static void AddFlyoutItems(Shell shell)
{
shell.FlyoutBehavior = FlyoutBehavior.Locked;
shell.Items.Add(new FlyoutItem { Title = "One", Items = { new ContentPage { Title = "One" } } });
shell.Items.Add(new FlyoutItem { Title = "Two", Items = { new ContentPage { Title = "Two" } } });
}

[Fact(DisplayName = "Shell Title Updates Correctly")]
public async Task ShellTitleUpdatesCorrectly()
{
Expand Down
Loading