From cbc9be8a8878676cfd72c18c51342c118d633ad7 Mon Sep 17 00:00:00 2001 From: Yimeng Wu Date: Sat, 12 Sep 2020 05:48:15 +0800 Subject: [PATCH] NavigationView: Fix crash when launched in Top mode with hierarchical navigation (microsoft/microsoft-ui-xaml#3166) --- .../NavigationView/NavigationViewItem.cs | 4 +++- .../ApiTests/NavigationViewTests.cs | 24 ++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/ModernWpf.Controls/NavigationView/NavigationViewItem.cs b/ModernWpf.Controls/NavigationView/NavigationViewItem.cs index b8bafccb..9e679d19 100644 --- a/ModernWpf.Controls/NavigationView/NavigationViewItem.cs +++ b/ModernWpf.Controls/NavigationView/NavigationViewItem.cs @@ -516,7 +516,9 @@ void UpdateVisualStateForChevron() internal bool HasChildren() { - return MenuItems.Count > 0 || (MenuItemsSource != null && m_repeater.ItemsSourceView.Count > 0) || HasUnrealizedChildren; + return MenuItems.Count > 0 + || (MenuItemsSource != null && m_repeater != null && m_repeater.ItemsSourceView.Count > 0) + || HasUnrealizedChildren; } bool ShouldShowIcon() diff --git a/test/ModernWpfTestApp/ApiTests/NavigationViewTests.cs b/test/ModernWpfTestApp/ApiTests/NavigationViewTests.cs index e9c5149c..3d2ef1e2 100644 --- a/test/ModernWpfTestApp/ApiTests/NavigationViewTests.cs +++ b/test/ModernWpfTestApp/ApiTests/NavigationViewTests.cs @@ -846,7 +846,7 @@ public void VerifyMenuItemAndContainerMappingMenuItemsSource() var navView = new NavigationView(); MUXControlsTestApp.App.TestContentRoot = navView; - navView.MenuItemsSource = new ObservableCollection { "Item 1", "Item 2" }; ; + navView.MenuItemsSource = new ObservableCollection { "Item 1", "Item 2" }; ; navView.Width = 1008; // forces the control into Expanded mode so that the menu renders MUXControlsTestApp.App.TestContentRoot.UpdateLayout(true); @@ -855,11 +855,11 @@ public void VerifyMenuItemAndContainerMappingMenuItemsSource() var menuItem = "Item 2"; // Get container for item var itemContainer = navView.ContainerFromMenuItem(menuItem) as NavigationViewItem; - bool correctContainerReturned = itemContainer != null && (itemContainer.Content as String) == menuItem; + bool correctContainerReturned = itemContainer != null && (itemContainer.Content as string) == menuItem; Verify.IsTrue(correctContainerReturned, "Correct container should be returned for passed in menu item."); // Get item for container - var returnedItem = navView.MenuItemFromContainer(itemContainer) as String; + var returnedItem = navView.MenuItemFromContainer(itemContainer) as string; bool correctItemReturned = returnedItem != null && returnedItem == menuItem; Verify.IsTrue(correctItemReturned, "Correct item should be returned for passed in container."); @@ -1080,5 +1080,23 @@ public void VerifyClearingItemsCollectionDoesNotCrashWhenItemSelectedOnTopNav() itemsSource.Clear(); }); } + + [TestMethod] + public void VerifyHierarchicalNavigationTopModeMenuItemsSourceDoesNotCrash() + { + RunOnUIThread.Execute(() => + { + var navView = new NavigationView(); + Content = navView; + + navView.PaneDisplayMode = NavigationViewPaneDisplayMode.Top; + + var childItem = new NavigationViewItem() { Content = "Item 1.1" }; + var parentItem = new NavigationViewItem() { Content = "Item 1", MenuItemsSource = new ObservableCollection() { childItem } }; + navView.MenuItemsSource = new ObservableCollection() { parentItem }; + + Content.UpdateLayout(); + }); + } } } \ No newline at end of file