Repository navigation
Description
Current behavior 🐛
Severity: Medium - apps that share one Flyout/MenuFlyout across several targets (a context menu reused by many rows) get a closed flyout instead of a moved one when they call ShowAt on a new target while it is open; no crash, but the menu just disappears.
Calling ShowAt(otherTarget) on a flyout that is currently open at a different target closes it for good. After things settle:
IsOpenisfalse- the content is unloaded (its last known position is
0,0) Openednever fires,Closedfires twice, andClosingis not raised
Observed event sequence after the second ShowAt:
content.Unloaded | Opening | content.Loaded | content.Unloaded | Closed | Closed
Expected behavior 🎯
Same as WinUI: the flyout closes at the old target and reopens at the new one, ending with IsOpen == true and the content placed next to the new target. The WinUI sequence is Closing | Closed | Opening | ... | Opened.
WinUI handles this in FlyoutBase::CheckAndHandleOpenFlyout: when the same flyout is shown at a different target, it hides the open one and stages the new request, then opens the staged flyout from OnPresenterUnloaded once the old presenter is gone:
CheckAndHandleOpenFlyout- "Hide previous instance and queue this one to be shown."OnPresenterUnloaded- raisesClosed, then opens the staged flyout ("This state can be reached if open flyout is re-opened with a different placement target").
The same steps on native WinUI 3 (Windows App SDK 2.5.1) end with IsOpen == true, the content loaded and placed next to the second button.
How to reproduce it (as minimally and precisely as possible) 🔬
- Create an app with two buttons,
A1andA2, at different positions. - Run:
var content = new Border { Width = 100, Height = 60 };
var flyout = new Flyout { Content = content };
flyout.ShowAt(A1);
await Task.Delay(500);
flyout.ShowAt(A2); // flyout still open at A1
await Task.Delay(700);
// WinUI: IsOpen == true, content shown next to A2
// Uno: IsOpen == false, content unloaded
Debug.WriteLine($"IsOpen={flyout.IsOpen} loaded={content.IsLoaded}");Runtime test that reproduces it (fails on Skia Desktop / Win32, master 46ea872727d, 3 of 3 attempts):
[TestMethod]
public async Task When_ShowAt_Other_Target_While_Open()
{
var a1 = new Button { Content = "A1", Width = 80, Height = 40, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top };
var a2 = new Button { Content = "A2", Width = 80, Height = 40, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top, Margin = new Thickness(250, 150, 0, 0) };
var root = new Grid { Width = 600, Height = 400, Children = { a1, a2 } };
var content = new Border { Width = 100, Height = 60 };
var flyout = new Flyout { Content = content };
WindowHelper.WindowContent = root;
await WindowHelper.WaitForLoaded(root);
flyout.ShowAt(a1);
await Task.Delay(500);
Assert.IsTrue(flyout.IsOpen);
flyout.ShowAt(a2);
await Task.Delay(700);
await WindowHelper.WaitForIdle();
Assert.IsTrue(flyout.IsOpen); // fails: False
Assert.IsTrue(content.IsLoaded); // fails: False
Assert.AreSame(a2, flyout.Target);
}Output: IsOpen=False content.IsLoaded=False target=A2 rect=0,0,100,60 events=[content.Unloaded | Opening | content.Loaded | content.Unloaded | Closed | Closed]
Workaround 🛠️
Call Hide(), wait for the Closed event, then call ShowAt with the new target.
Affected platforms 📱💻🖥️
All platforms 🌍 (shared FlyoutBase code; runtime-verified on Desktop (Windows))
Uno.Sdk version (and other relevant versions) 📦
Uno master 46ea872727d (net11.0-desktop, .NET SDK 11.0.100)
IDE version 🧑💻
N/A
Anything else we need to know? 💬
Root cause (source-backed, and matches the runtime event trace):
FlyoutBase.ShowAtCore handles "open at another target" by calling Hide(canCancel: false) and then reopening synchronously. Hide ends in RemoveFromOpenFlyouts, which removes the flyout from _openFlyouts and posts a dispatcher callback that raises Closed and then calls _openFlyouts[0].Hide().
By the time that callback runs, the same flyout has already re-added itself through AddToOpenFlyouts, so _openFlyouts[0] is the flyout that was just reopened, and the deferred callback hides it. That is where the second Closed comes from.
A related problem: while that callback is pending, _isClosedPending is true, and ShowAtCore returns early, so any ShowAt made in that window does nothing.
Suggested fix: follow WinUI's staged-show model. When ShowAt targets a different element while open, hide the flyout and remember the pending request (a "staged" flyout plus its target). Open it once the close has completed, instead of reopening synchronously and letting the deferred cleanup hide whichever flyout is first in _openFlyouts. At minimum, the deferred callback shouldn't hide a flyout that was opened after the callback was posted.
Related: #26398 (another FlyoutBase open/close lifecycle gap: the flyout stays open after its placement target is removed).
Activity