Skip to content

CommandBar oscillates open/closed when reopened from its own Closed handlerΒ #24671

In review

Description

@MartinZikmund

Current behavior πŸ›

Reopening a CommandBar from its own Closed event handler β€” a pattern WinUI explicitly documents as supported β€” makes the CommandBar and its overflow popup oscillate open/closed in a tight loop instead of settling open. Closed fires thousands of times per second; a runtime test I wrote counted 2622 Closed invocations in 1.5 seconds for a single close-then-reopen. The bar does eventually read IsOpen == true, but only after burning CPU in a loop the whole time.

Root cause: CommandBar subscribes to its own overflow Popup's Closed event and unconditionally forces IsOpen = false whenever that popup closes β€” including when the popup closed because the CommandBar itself just closed normally. Since Popup.Closed is always raised through a deferred dispatch (not synchronously), this queued IsOpen = false lands after an app's Closed handler has already reopened the bar, undoing the reopen and immediately re-triggering the whole cycle.

src/Uno.UI/UI/Xaml/Controls/CommandBar/CommandBar.Partial.cs:

// lines 277-279 (Uno Only)
m_tpOverflowPopup.Closed += OnOverflowPopupClosed;
m_overflowPopupClosedEventHandler.Disposable = Disposable.Create(() => m_tpOverflowPopup.Closed -= OnOverflowPopupClosed);

// lines 339-342 (Uno Only)
private void OnOverflowPopupClosed(object? sender, object e)
{
    NativeDispatcher.Main.Enqueue(() => IsOpen = false, NativeDispatcherPriority.Idle);
}

CommandBar.OnClosed (lines 1086-1103) already closes the popup itself as part of every normal close:

protected override void OnClosed(object e)
{
    SetCompactMode(true);

    if (m_tpOverflowPopup is { })
    {
        m_tpOverflowPopup.IsOpen = false;
    }

    // We need to call this last, rather than first (the usual pattern),
    // because this raises an event that apps can use to set IsOpen = true.
    // ...
    base.OnClosed(e);
}

So every normal close of the CommandBar also closes the popup, which raises the popup's Closed event, which queues a deferred, unconditional IsOpen = false on the CommandBar β€” even though the bar may have just been reopened by the app's own Closed handler (the exact scenario the comment above says is supported). That queued IsOpen = false fires, closes the bar again (closing the popup again, queuing another deferred IsOpen = false), the app's handler reopens it again, and so on.

Expected behavior 🎯

WinUI has no such mapping between the overflow popup's close and CommandBar.IsOpen. CommandBar::OnApplyTemplate only sets IsSubMenu on the popup (CommandBar_Partial.cpp#L348-L355); nothing hooks the popup's Closed event to force CommandBar.IsOpen. CommandBar::OnClosedImpl only closes the popup and then raises the Closed event β€” it explicitly documents that apps can set IsOpen = true from that event (CommandBar_Partial.cpp#L1433-L1453):

_Check_return_ HRESULT
CommandBar::OnClosedImpl(_In_ IInspectable* pArgs)
{
    IFC_RETURN(SetCompactMode(true));

    if (m_tpOverflowPopup)
    {
        IFC_RETURN(m_tpOverflowPopup->put_IsOpen(FALSE));
    }

    // We need to call this last, rather than first (the usual pattern),
    // because this raises an event that apps can use to set IsOpen = true.
    // If that happened before the above, then we would effectively
    // see a sequence of Open -> Open -> Close, instead of
    // Open -> Close -> Open, which gets us into a state where
    // CompactMode is still set to true even when the CommandBar is open,
    // hiding AppBarButton labels and the overflow popup.
    IFC_RETURN(__super::OnClosedImpl(pArgs));

    return S_OK;
}

The CommandBar should settle open immediately, without any looping, when an app reopens it from Closed.

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

  1. Create a CommandBar and subscribe to its Closed event; in the handler, set commandBar.IsOpen = true.
  2. Open the CommandBar (IsOpen = true) and wait for it to settle.
  3. Close it (IsOpen = false).
  4. Observe that Closed keeps firing repeatedly (thousands of times per second) for well over a second before the bar settles open, instead of reopening once, immediately.

Minimal repro:

var commandBar = new CommandBar();
commandBar.PrimaryCommands.Add(new AppBarButton { Icon = new SymbolIcon(Symbol.Add) });

var closedCount = 0;
commandBar.Closed += (s, e) =>
{
    closedCount++;
    ((CommandBar)s).IsOpen = true;
};

// Host commandBar in a page, e.g. as Page.TopAppBar or Page.BottomAppBar.

commandBar.IsOpen = true;
// ... wait for it to open ...
commandBar.IsOpen = false;
// closedCount climbs into the thousands within ~1.5s instead of settling at 1.

I confirmed this with a runtime test built on the existing CanReopenInClosedHandler test in CommandBarIntegrationTests, extended to poll for ~1.5 seconds (15 iterations of WaitForIdle + a 100ms delay) after closing, and count Closed invocations β€” it logged 2622 invocations for that single window, with cmdBar.IsOpen finally reading true at the end.

Workaround πŸ› οΈ

None that avoids the CPU churn. The bar does eventually settle in the IsOpen == true state, so the visible symptom may only be a stutter/CPU spike rather than a permanently broken UI, but any app using the documented reopen-from-Closed pattern pays this cost every time.

Renderer 🎨

  • Skia

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

All platforms 🌍 (the code lives in the shared, non-suffixed CommandBar.Partial.cs; measured on Desktop (Windows) Skia)

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

Uno master @ commit 3950a923f5f457ec5b86cf52d654dad20ded75c6 (7.0-dev), built with .NET SDK 11.0.100-rc.1.26425.128, target net11.0-desktop.

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

N/A (built and run from the CLI)

Anything else we need to know? πŸ’¬

Root cause and suggested fix: delete the m_tpOverflowPopup.Closed += OnOverflowPopupClosed subscription and the OnOverflowPopupClosed handler entirely (src/Uno.UI/UI/Xaml/Controls/CommandBar/CommandBar.Partial.cs lines 277-279 and 339-342) β€” this moves the port closer to the WinUI C++ source, which has no such mapping. At minimum, the handler should be guarded so it only reacts to a popup close the CommandBar did not itself just perform.

The handler's own comment claims it exists because "AppBar/CommandBar will not light-dismiss close unless we listen to the Closed event for the overflow Popup." That no longer appears to be necessary: PopupPanel.OnPointerPressed (src/Uno.UI/UI/Xaml/Controls/Popup/PopupPanel.cs, around lines 212-223) already calls CommandBar.TryDismissInlineAppBarInternal() directly for a press outside the popup. Removing the handler should be verified against the light-dismiss and focus-navigation tests in CommandBarIntegrationTests before merging, since I only confirmed the oscillation itself, not the full blast radius of removing the handler.

Activity

  1. added
    kind/bug
    triage/untriaged
    platform/all
    difficulty/medium πŸ€”
    area/popups πŸ’¬
    area/commandbar
    on Sep 25, 2026
  2. added
    7.1
    on Oct 14, 2026
  3. removed
    7.1
    on Oct 29, 2026
  4. self-assigned this
    on Nov 24, 2026
  5. added theissue type on Nov 25, 2026
  6. removed
    kind/bug
    difficulty/medium πŸ€”
    on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/commandbararea/popups πŸ’¬platform/alltriage/untriaged

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions