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 crash when launched in Top mode with hierarchical navigation #3166

Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion dev/NavigationView/NavigationViewItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,9 @@ void NavigationViewItem::UpdateVisualStateForChevron()

bool NavigationViewItem::HasChildren()
{
return MenuItems().Size() > 0 || (MenuItemsSource() != nullptr && m_repeater.get().ItemsSourceView().Count() > 0) || HasUnrealizedChildren();
return MenuItems().Size() > 0
|| (MenuItemsSource() != nullptr && m_repeater != nullptr && m_repeater.get().ItemsSourceView().Count() > 0)
|| HasUnrealizedChildren();
}

bool NavigationViewItem::ShouldShowIcon()
Expand Down
27 changes: 24 additions & 3 deletions dev/NavigationView/NavigationView_ApiTests/NavigationViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -681,19 +681,19 @@ public void VerifyMenuItemAndContainerMappingMenuItemsSource()
var navView = new NavigationView();
MUXControlsTestApp.App.TestContentRoot = navView;

navView.MenuItemsSource = new ObservableCollection<String> { "Item 1", "Item 2" }; ;
navView.MenuItemsSource = new ObservableCollection<string> { "Item 1", "Item 2" };
navView.Width = 1008; // forces the control into Expanded mode so that the menu renders

MUXControlsTestApp.App.TestContentRoot.UpdateLayout();

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.");

Expand Down Expand Up @@ -872,5 +872,26 @@ public void VerifyOverflowButtonToolTip()
Verify.IsTrue(testCondition, "ToolTip text should have been \"More\".");
});
}

[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<NavigationViewItem>() { childItem } };
navView.MenuItemsSource = new ObservableCollection<NavigationViewItem>() { parentItem };

Content.UpdateLayout();

// If we don't get here, app has crashed. This verify signals the NavigationView was launched in Top mode without crashing.
Verify.IsTrue(true);
});
}
}
}