Skip to content

Commit

Permalink
NavigationView: Fix app crash in Top mode when clearing the item coll…
Browse files Browse the repository at this point in the history
…ection while a selected item exists (microsoft/microsoft-ui-xaml#3202)
  • Loading branch information
Kinnara committed Sep 6, 2020
1 parent 625ac5c commit c5e16e1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
5 changes: 3 additions & 2 deletions ModernWpf.Controls/NavigationView/NavigationView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,10 @@ void OnSelectionModelSelectionChanged(SelectionModel selectionModel, SelectionMo

if (IsTopNavigationView())
{
var inMainMenu = selectedIndex.GetAt(0) == c_mainMenuBlockIndex;
// If selectedIndex does not exist, means item is being deselected through API
var isInOverflow = (selectedIndex != null && selectedIndex.GetSize() > 0) ? inMainMenu && !m_topDataProvider.IsItemInPrimaryList(selectedIndex.GetAt(1)) : false;
var isInOverflow = (selectedIndex != null && 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 test/ModernWpfTestApp/ApiTests/NavigationViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -879,5 +879,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();
});
}
}
}

0 comments on commit c5e16e1

Please sign in to comment.