microsoft / microsoft/microsoft-ui-xaml

NavigationView crashes when collapsing/expanding after modifying items dynamically

Open
#10,757 0 comments 0 reactions 0 assignees View on GitHub
area-NavigationView bug team-Controls
Dominant language
C++
Stars
8.4k
Forks
942
Avg merge
2d 7h
Merged PRs (30d)
105

Description

### Describe the bug

When dynamically updating items (adding new items in the `NavigationView` or inside other `NavigationViewItems`, removing, or editing (renaming, changing properties) these items) and refreshing there is a seemingly random chance that the program will crash after collapsing the Navigation View with this error:

```
Exception thrown at 0x00007FFA38F366CA in Workflow.exe: Microsoft C++ exception: winrt::hresult_error at memory location 0x000000B7399D7240.
```

The debugger redirects me to `App.i.g.cs` (note that this file has remained unchanged). Specifically, it highlights these lines:
```cs
global::Microsoft.UI.Xaml.Application.Start((p) => {
var context = new global::Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext(global::Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread());
global::System.Threading.SynchronizationContext.SetSynchronizationContext(context);
new App();
});
```

> [!NOTE]
> You might need to check "Win32 exceptions" and "Common Language Runtime Exceptions" in Visual Studio's Exception Settings to see this. Otherwise, the debugger redirects to `global::System.Diagnostics.Debugger.Break();` in `App.i.g.cs` with no error.

### Why is this important?

My specific use case:
I'm making a productivity app, where the user can create Workspaces and Folders contained within these Workspaces. The Folders can also be customized so it's easier to organize them using colors, icons, etc. The navigation between Workspaces and Folders is done through a NavigationView, since it's the most user-friendly and straightforward way to navigate between pages like this, and its built-in tree-like structure makes it easy to group pages.

Adding, removing or editing items in a NavigationView has numerous other use cases, like for file explorers, item lists, etc. I find it a core feature of an app like this, so I consider this bug to be important enough.

### Steps to reproduce the bug

1.Create a NavigationView with a NavigationViewItem (example “Workspaces”) that contains subitems.
2. Dynamically add/remove subitems using code-behind (e.g., by enumerating folders in ApplicationData.Current.LocalFolder).
3. Open the app, expand/collapse the NavigationView.
4. After editing/deleting/renaming a workspace, collapse the NavigationView pane → crash.

-----

I have made a minimal reproduction project in this repository: https://github.com/Georgios1999/NavViewCrashRepro.git

Build it in Visual Studio 2022, test it by adding, removing, recoloring, editing workspaces and folders and as you do, try to collapse the Navigation View by clicking this built-in button:
Image
The app should eventually crash. This doesn’t always happen immediately, but after a few refresh cycles (add/remove/recolor + collapse/expand), the crash reliably occurs.

### Actual behavior

App crashes with an unhandled `winrt::hresult_error` inside `Microsoft.UI.Xaml.dll` when collapsing/expanding the NavigationView. (See details and additional info for more info)

### Expected behavior

The NavigationView collapses as expected.

### Screenshots

_No response_

### NuGet package version

WinUI 3 - Windows App SDK 1.7.3: 1.7.250606001

### Windows version

Windows Insider Build (xxxxx)

### Additional context

Debug also shows
```
Microsoft.UI.Xaml.dll!00007FF9D855A6FF: 80004005 - Unspecified error
Exception thrown at 0x00007FFA38F366CA (KernelBase.dll) in Workflow.exe: WinRT originate error - 0x80004005 : 'Unspecified error'.
```

-----

### Extra info:
**Windows SDK:** 10.0.26100.4654
**IDE:** Visual Studio 2022 (latest stable)
**Target framework:** net8.0-windows10.0.19041.0

-----

> [!NOTE]
> The same exception is mentioned in #9538 but I decided to create this issue to submit more info and to document what happens in my use case. Thought that would help. If not, feel free to close this issue.

Contributor guide

Open the contributing guide

Research direction

Start by building the linked NavViewCrashRepro project in Visual Studio 2022 with Windows App SDK 1.7.3, then reproduce the crash by dynamically adding, removing, or editing NavigationView items while collapsing and expanding the pane. Use the reported winrt::hresult_error and Microsoft.UI.Xaml.dll failure to trace the relevant NavigationView behavior. Done means the described dynamic-item workflow no longer crashes.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, csharp
Domain
desktop, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.