Repository navigation
Description
Current behavior
I create new pages and dynamically (from the code behind) add the NavigationViewItem corresponding to this page. When the page is closed by the user (the "X" button placed on the page), the previously added NavigationViewItem is also removed. The first closing of the page and removal of the NavigationViewItem is successful. All subsequent attempts lead to the following situation: NavigationViewItem is removed from MenuItems with the next error
Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')".
Call stack - at System.Collections.Generic.List`1.get_Item(Int32 index)
at System.Collections.ObjectModel.Collection`1.get_Item(Int32 index)
at Microsoft.UI.Xaml.Controls.InspectingDataSource.GetAtCore(Int32 index) in C:\a\1\s\src\Uno.UI\Microsoft\UI\Xaml\Controls\Repeater\InspectingDataSource.cs:line 90
at Microsoft.UI.Xaml.Controls.ItemsSourceView.GetAt(Int32 index) in C:\a\1\s\src\Uno.UI\Microsoft\UI\Xaml\Controls\Repeater\ItemsSourceView.cs:line 44
at Microsoft.UI.Xaml.Controls.SelectionModel.<>c.<get_SelectedItems>b__17_1(IList`1 infos, Int32 index) in C:\a\1\s\src\Uno.UI\Microsoft\UI\Xaml\Controls\Repeater\SelectionModel.cs:line 201
at Microsoft.UI.Xaml.Controls.SelectedItems`1.get_Item(Int32 index) in C:\a\1\s\src\Uno.UI\Microsoft\UI\Xaml\Controls\Repeater\SelectedItems.cs:line 37
at Microsoft.UI.Xaml.Controls.SelectionModel.get_SelectedItem() in C:\a\1\s\src\Uno.UI\Microsoft\UI\Xaml\Controls\Repeater\SelectionModel.cs:line 150
at Microsoft.UI.Xaml.Controls.NavigationView.OnSelectionModelSelectionChanged(SelectionModel selectionModel, SelectionModelSelectionChangedEventArgs e) in C:\a\1\s\src\Uno.UI\Microsoft\UI\Xaml\Controls\NavigationView\NavigationView.cs:line 282
at Microsoft.UI.Xaml.Controls.SelectionModel.OnSelectionChanged() in C:\a\1\s\src\Uno.UI\Microsoft\UI\Xaml\Controls\Repeater\SelectionModel.cs:line 572
at Microsoft.UI.Xaml.Controls.SelectionModel.OnSelectionInvalidatedDueToCollectionChange() in C:\a\1\s\src\Uno.UI\Microsoft\UI\Xaml\Controls\Repeater\SelectionModel.cs:line 494
at Microsoft.UI.Xaml.Controls.SelectionNode.OnSourceListChanged(Object dataSource, NotifyCollectionChangedEventArgs args) in C:\a\1\s\src\Uno.UI\Microsoft\UI\Xaml\Controls\Repeater\SelectionNode.cs:line 515
at Microsoft.UI.Xaml.Controls.ItemsSourceView.OnItemsSourceChanged(NotifyCollectionChangedEventArgs args) in C:\a\1\s\src\Uno.UI\Microsoft\UI\Xaml\Controls\Repeater\ItemsSourceView.cs:line 62
at Microsoft.UI.Xaml.Controls.InspectingDataSource.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) in C:\a\1\s\src\Uno.UI\Microsoft\UI\Xaml\Controls\Repeater\InspectingDataSource.cs:line 165
at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
at System.Collections.ObjectModel.ObservableCollection`1.RemoveItem(Int32 index)
at System.Collections.ObjectModel.Collection`1.RemoveAt(Int32 index)
at AxisUno.Views.MainView.PageClose(String pageId) in /home/microinvest/RiderProjects/AxisUno/AxisUno.Shared/Views/MainView.xaml.cs:line 241
It works correctly on Windows, but makes a mistake on Linux!
Expected behavior
NavigationViewItem is removed successfully (without errors) from the MenuItems of NavigationView
How to reproduce it (as minimally and precisely as possible)
- Create project: dotnet new unoapp-winui-net6 -o MyUnoApp
- Open project in VS 2022 (Windows) and create MainPage with NavigationView
- Create several pages with button to close page (behavior like button to close Form in WinForms)
- Click by existing NavigationViewItem create new page and new NavigationViewItem below
- Next steps are described above
Workaround
No response
Works on UWP/WinUI
Yes
Environment
No response
NuGet package version(s)
Uno.CommunityToolkit.WinUI.UI.Behaviors" Version="7.1.100-dev.7.g758154cb81"
Uno.Microsoft.Xaml.Behaviors.WinUI.Managed" Version="2.3.0"
Uno.WinUI.Skia.Gtk" Version="4.1.9"
Uno.WinUI.RemoteControl" Version="4.1.9" Condition="'$(Configuration)'=='Debug'"
Uno.UI.Adapter.Microsoft.Extensions.Logging" Version="4.1.9"
Uno.CommunityToolkit.WinUI.UI.Controls.DataGrid" Version="7.1.100-dev.7.g758154cb81"
Uno.CommunityToolkit.WinUI.UI.Controls.Layout" Version="7.1.100-dev.7.g758154cb81"
Affected platforms
Skia (GTK on Linux/macOS/Windows)
IDE
Visual Studio 2022
IDE version
No response
Relevant plugins
No response
Anything else we need to know?
I use Rider to run my app on Linux.
Activity
jeromelaban commentedon Apr 21, 2022
Thanks for the report. Could you create a small repro app which shows the issue, so we can discuss the exact same behavior?
SergeyRoznyuk commentedon Apr 26, 2022
Send you link to small app to show the issue: https://github.com/OnSwitchOff/MyUnoAppSer.git. The issue is in PageClose method (MainPage).
I've added TextBox, DatePicker and CalendarDatePicker due to behaviour of them on Linux:
TextBox - pointer is not visible (maybe it has white foreground) so I Can't see position in which I will write;
DatePicker - selection date is not correct sometimes;
CalendarDatePicker - I can't change month.
[-]Linux-NavigationView-Can't remove NavigationViewItem from [/-][+][GTK Linux] `NavigationView` - Can't remove `NavigationViewItem`[/+]Youssef1313 commentedon Nov 22, 2023
Minimized repro:
XAML:
Code behind:
IMPORTANT: This is only reproducible with MUXC version of NavigationView. So, for testing in samples app, make sure to include
muxcxmlns and use it.Youssef1313 commentedon Nov 22, 2023
Use this commit for easier debugging in Uno.
Youssef1313 commentedon Nov 22, 2023
It's getting tricky to understand what is going on.
One important observation is
ItemsSourceViewmaintains a count cache that gets out of sync with the actual count; or more specifically, it's likely that we are in a state where we are using the count cache and it's not yet updated (but it will). This could happen for example if there are two subscribers to collection change:However, even if I try to give up using the cache and instead get the count directly, the code still crashes, but happens at a later point with different stack trace.
Youssef1313 commentedon Nov 22, 2023
Here is why the count cache was out of sync.
There are (at least) two instances of ItemsSourceView that are operating on the SAME observable collection.
Both subscribe to collection changes for sure. What happens is:
The stack trace is large so split into two screenshots:
Youssef1313 commentedon Nov 22, 2023
In the above stack trace, there is a measure happening which DOES NOT happen in WinUI.
MartinZikmund commentedon May 29, 2025
@morning4coffe-dev can you validate this is not happening on current Desktop targets? In such case we could close
NavigationView#21054MartinZikmund commentedon Apr 8, 2026
Repro generator
This issue was classified as not reproducible with a code sample or test.
Reason: GTK Linux-specific NavigationView item removal bug.
test: Cover NavigationView fixes with runtime tests
test: Cover NavigationView fixes with runtime tests