microsoft-ui-xaml icon indicating copy to clipboard operation
microsoft-ui-xaml copied to clipboard

Old window gains focus and moves to foreground when opening new window in `NavigationView.ItemInvoked`

Open Scighost opened this issue 11 months ago • 4 comments

Describe the bug

When handling the NavigationView.ItemInvoked event to open a new window, the old window unexpectedly regains focus and moves to the foreground after the new window is shown. This disrupts the user experience as the old window should remain in its current state and not interfere with the new window. However, this issue does not occur when opening a new window inside the NavigationViewItem.Tapped event.

Steps to reproduce the bug

<Window x:Class="App.MainWindow">
    <NavigationView ItemInvoked="NavigationView_ItemInvoked">
        <NavigationView.PaneHeader>
            <NavigationViewItem Content="Tapped"
                                Icon="Accept"
                                Tapped="NavigationViewItem_Tapped" />
        </NavigationView.PaneHeader>
        <NavigationView.MenuItems>
            <NavigationViewItem Content="ItemInvoked" Icon="Cancel" />
        </NavigationView.MenuItems>
    </NavigationView>
</Window>
private void NavigationView_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
{
    new MainWindow().Activate();
}

private void NavigationViewItem_Tapped(object sender, TappedRoutedEventArgs e)
{
    new MainWindow().Activate();
}

Expected behavior

The new window should open and gain focus without affecting the old window's focus or Z-order.

Screenshots

https://github.com/user-attachments/assets/dd8cb9f7-f7f5-4773-a589-8aecc7d76cfd

NuGet package version

WinUI 3 - Windows App SDK 1.6.3: 1.6.241114003

Windows version

Windows 11 (23H2): Build 22631

Additional context

No response

Scighost avatar Jan 05 '25 15:01 Scighost

This looks like an issue on the Activated method on the Window class, not NavigationView.

karkarl avatar Jan 13 '25 06:01 karkarl

Yea, I'm seeing the same thing in my own app, though I'm seeing it in a ListViewItem.DoubleTapped handler. Curiously, a ListView.ItemClicked or just a plain old ListViewItem.Tapped doesn't hit this?

zadjii-msft avatar Feb 12 '25 20:02 zadjii-msft

@Scighost see L#2693

https://github.com/microsoft/microsoft-ui-xaml/blob/589ced40542b9b4de2fddf0e58a1e3eecd164e2f/src/controls/dev/NavigationView/NavigationView.cpp#L2690-L2694

I guess you should delay the window creation

Lightczx avatar Feb 18 '25 09:02 Lightczx

This bug just bit me too.

I found a workaround. In my case, its ItemsView's ItemInvoked (and DoubleTapped for ListView also has this bug), but I think its pretty much the same for NavigationView's ItemInvoked.

I realized that when I "invoked" an item by double clicking, the window showed up before the last mouse "pointer release" event. And the main window got focused after the second/last mouse "pointer release" event. So, it looked like the last mouse "pointer release" event stole the focus.

So, I added e.Handled = true; on PointerPressed event and it worked. Originally, I try to delay the window creation like someone else said, but it does not always work because it relies on timing of PointerReleased event. Also, AllowFocusOnInteraction="False" does not work either.

XAML:

<ItemsView ItemInvoked="ItemsView_ItemInvoked">
    <ItemsView.ItemTemplate>
        <DataTemplate x:DataType="...">
            <ItemContainer PointerPressed="ItemContainer_PointerPressed" PointerReleased="ItemContainer_PointerReleased">
...
            </ItemContainer>
        </DataTemplate>
    </ItemsView.ItemTemplate>
</ItemsView>

Code behind:

    private void ItemContainer_PointerPressed(object sender, Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e)
    {
        e.Handled = true;
    }
    private void ItemContainer_PointerReleased(object sender, Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e)
    {
        e.Handled = true;
    }
    private void ItemsView_ItemInvoked(ItemsView sender, ItemsViewItemInvokedEventArgs args)
    {
        // Create and show new window
    }

Reference: https://github.com/microsoft/microsoft-ui-xaml/issues/8087 https://github.com/microsoft/microsoft-ui-xaml/issues/8520 https://github.com/microsoft/microsoft-ui-xaml/issues/5411

torum avatar Nov 15 '25 05:11 torum