Repository navigation
Description
Current behavior
Severity: Medium - Anyone removing a flyout's placement target while the flyout is open ends up with an orphaned open flyout (and light-dismiss layer) that stays on screen and keeps the target and owner alive.
When the element a Flyout was shown at (ShowAt(target)) is removed from the visual tree while the flyout is open, the flyout stays open: IsOpen remains true, its content stays loaded, the popup is still listed by VisualTreeHelper.GetOpenPopupsForXamlRoot, and no Closing/Closed events are raised.
Expected behavior
The flyout closes (Opened | Closing | Closed, content unloaded, 0 open popups), as on WinUI.
Backed by WinUI C++ source: FlyoutBase::SetPlacementTarget attaches an Unloaded handler on the placement target that calls Hide() ("Hook up an event handler that will dismiss the flyout if the placement target gets unloaded"): https://github.com/microsoft/microsoft-ui-xaml/blob/main/dxaml/xcp/dxaml/lib/FlyoutBase_Partial.cpp (FlyoutBase::SetPlacementTarget).
The same scenario was also run on real WinUI 3 (WinAppSDK 2.5.1): IsOpen=False, content not loaded, events Opened | Closing | Closed.
How to reproduce it
- Create a page with a
Grid(host) containing aButton(a1). - Run:
var fly = new Flyout { Content = new Border { Width = 100, Height = 60 } };
fly.ShowAt(a1);
await Task.Delay(500);
host.Children.Remove(a1);
await Task.Delay(700);
// fly.IsOpen: WinUI False, Uno TrueRuntime result (Uno Skia desktop, Release build of master 4636b705e97):
isOpen.afterTargetRemoved = True (WinUI: False)
openPopups.afterTargetRemoved = 1 (WinUI: 0)
events = Opening | content.Loaded | Opened (WinUI: ... | Closing | Closed | content.Unloaded)
FlyoutBase.cs is unchanged between that build and current origin/master.
Workaround
Hide the flyout in the target's Unloaded handler.
Works on UWP/WinUI
Yes
Environment
Nuget Package: master 4636b705e97 (net11.0-desktop)
Affected platforms
Skia (Desktop) observed at runtime; the code is shared FlyoutBase, so likely all heads.
Anything else
Root cause (source-backed): FlyoutBase.ShowAtCore only subscribes ActualThemeChanged on the placement target; nothing reacts to the target leaving the tree.
https://github.com/unoplatform/uno/blob/46ea872727d/src/Uno.UI/UI/Xaml/Controls/Flyout/FlyoutBase.cs#L544-L548
Suggested fix: mirror WinUI by subscribing Unloaded on the target next to ActualThemeChanged (and unsubscribing in the same places, around L397 and L441), and call Hide() from the handler.
Activity
christo-polydorou commentedon Nov 21, 2026
I'd like to pick this one up. Here's the plan before I open a PR:
Fix. Mirror WinUI's
FlyoutBase::SetPlacementTarget: while the flyout is open, subscribe to the placement target'sUnloadedand callHide()from it. This goes inFlyoutBase.ShowAtCore, next to the existingActualThemeChangedsubscription. Both handlers are detached in the same two places as today (whenTargetis reassigned and inHide), so the target still doesn't keep the flyout alive after it closes.Two small details:
Hide()whenIsOpenis true. That way an unload after the flyout has already closed doesn't raise a secondClosing/Closed.Openingis cancelled, the handlers stay attached, and the nextShowAtwould subscribe them a second time.Because it goes through the public
Hide(), the sequence isOpened | Closing | Closed, the same as the WinUI 3 run in the description.Closingcan still be cancelled, as in WinUI.Tests (runtime, in
Given_Flyout):When_Target_Removed_While_Open_Closes: removing the target closes the flyout.IsOpenbecomes false, the content unloads, there are 0 open popups, and the events areOpened | Closing | Closed.When_Target_Removed_After_Closed_No_Extra_Events: removing the target after a normal close raises no extra events.Out of scope. #26399 (re-
ShowAtat a new target) and #26400 (only one open flyout at a time) need a port of WinUI's open/staged flyout metadata (CheckAndHandleOpenFlyout, staged open fromOnPresenterUnloaded). I'll handle those in separate PRs so this one stays a small starter fix. Nothing here should get in the way of that port.fix(flyout): Close flyout when placement target unloads