Skip to content

[Skia] ButtonBase and MenuFlyoutItem: a long-lived Command keeps never-loaded controls alive through CanExecuteChangedΒ #25969

Closed

Description

@Xiaoy312

Current behavior πŸ›

ButtonBase (Button, AppBarButton, …) and MenuFlyoutItem (ToggleMenuFlyoutItem, RadioMenuFlyoutItem, …) subscribe to Command.CanExecuteChanged with a strong handler. The handler is only removed when Command changes or when the control leaves the live tree.

A control that never enters the live tree never removes it, so a command that lives longer than the control keeps the control alive. A view model's command is a typical example. Common cases:

  • The MenuFlyoutItems of a MenuFlyout that is never opened. They also keep the whole page alive, through their DataContext and parents, even after the page is shown and closed.
  • A Button with a Command that is created but never shown.

Expected behavior 🎯

A command does not keep its controls alive, as on WinUI, which holds this subscription weakly (WeakEventPtr). A control that is still alive keeps updating IsEnabled from CanExecute, loaded or not.

How to reproduce it (as minimally and precisely as possible) πŸ”¬

var command = new MyCommand(); // long-lived, e.g. on an app-wide view model
var refs = new List<WeakReference>();
for (var i = 0; i < 100; i++)
{
    var menu = new MenuFlyout();
    menu.Items.Add(new MenuFlyoutItem { Text = "Delete", Command = command });
    var page = new Grid { Children = { new Button { Content = "...", Flyout = menu } } };
    refs.Add(new WeakReference(page)); // never shown, or shown then removed
}

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

// Expected 0, actual 100: every page is rooted by command.CanExecuteChanged
Console.WriteLine(refs.Count(r => r.IsAlive));

Workaround πŸ› οΈ

Clear Command on the items before dropping the page. Or use an ICommand whose CanExecuteChanged holds its handlers weakly.

Affected platforms πŸ“±πŸ’»πŸ–₯️

All platforms 🌍

Uno.Sdk version (and other relevant versions) πŸ“¦

Reproduced on Uno.Sdk 6.7.30 and 7.0.0-dev.130.

IDE version πŸ§‘β€πŸ’»

No response

Anything else we need to know? πŸ’¬

Not a regression from a recent change. The subscription has been strong since these controls were ported.

Activity

  1. added
    triage/untriaged
    on Nov 3, 2026
  2. added theissue type on Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage/untriaged

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions