Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NavigationView: Fix app crash in Top mode when clearing the item collection while a selected item exists #3202

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions dev/NavigationView/NavigationView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,10 @@ void NavigationView::OnSelectionModelSelectionChanged(const winrt::SelectionMode

if (IsTopNavigationView())
{
auto const inMainMenu = selectedIndex.GetAt(0) == c_mainMenuBlockIndex;
// If selectedIndex does not exist, means item is being deselected through API
auto const isInOverflow = (selectedIndex && selectedIndex.GetSize() > 0) ? inMainMenu && !m_topDataProvider.IsItemInPrimaryList(selectedIndex.GetAt(1)) : false;
Felix-Dev marked this conversation as resolved.
Show resolved Hide resolved
auto const isInOverflow = (selectedIndex && selectedIndex.GetSize() > 1)
? selectedIndex.GetAt(0) == c_mainMenuBlockIndex && !m_topDataProvider.IsItemInPrimaryList(selectedIndex.GetAt(1))
: false;
if (isInOverflow)
{
// We only want to close the overflow flyout and move the item on selection if it is a leaf node
Expand Down
42 changes: 42 additions & 0 deletions dev/NavigationView/NavigationView_ApiTests/NavigationViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -872,5 +872,47 @@ public void VerifyOverflowButtonToolTip()
Verify.IsTrue(testCondition, "ToolTip text should have been \"More\".");
});
}

[TestMethod]
public void VerifyClearingItemsCollectionDoesNotCrashWhenItemSelectedOnTopNav()
{
RunOnUIThread.Execute(() =>
{
var navView = new NavigationView();
navView.PaneDisplayMode = NavigationViewPaneDisplayMode.Top;

var navViewItem1 = new NavigationViewItem() { Content = "MenuItem 1", };
var navViewItem2 = new NavigationViewItem() { Content = "MenuItem 2" };

Log.Comment("Set up the MenuItems collection");
navView.MenuItems.Add(navViewItem1);
navView.MenuItems.Add(navViewItem2);

Content = navView;
Content.UpdateLayout();

Log.Comment("Set MenuItem 1 as selected");
navView.SelectedItem = navViewItem1;
Verify.AreEqual(navViewItem1, navView.SelectedItem, "MenuItem 1 should have been selected");

// Clearing the MenuItems collection should not crash the app
Log.Comment("Clear the MenuItems collection");
navView.MenuItems.Clear();

Log.Comment("Set up the MenuItemsSource collection");
var itemsSource = new ObservableCollection<NavigationViewItem>() { navViewItem1, navViewItem2 };
navView.MenuItemsSource = itemsSource;

Content.UpdateLayout();

Log.Comment("Set MenuItem 1 as selected");
navView.SelectedItem = navViewItem1;
Verify.AreEqual(navViewItem1, navView.SelectedItem, "MenuItem 1 should have been selected");

// Clearing the MenuItemsSource collection should not crash the app
Log.Comment("Clear the MenuItemsSource collection");
itemsSource.Clear();
});
}
}
}