We setup the SelectionChanged event of NavigationView to run a few conditions check.
private void ApplicationNavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
}
We would only allow the user to navigate from Page A to Page B when the conditions are met.
The problem is even if we can stop the navigation from Page A to Page B, the selection indicator of NavigationView
always changes from NavigationViewItem
A (Page A) to NavigationViewItem
B (Page B) upon user click/tap. This is misleading because selection indicator shows NavigationViewItem
B is selected, whereas the pages remains to be Page A.
Is there any way to cancel the SelectionChanged event and stop selection indicator change?
Tried with following code without success
private NavigationViewItem previousNavigationViewItemSelected = null;
private async void ApplicationNavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
var selectedItem = (args.SelectedItem) as NavigationViewItem;
if (selectedItem.Tag != null)
{
string pageName = "NavigationViewTest." + ((string)selectedItem.Tag);
Type pageType = Type.GetType(pageName);
contentFrame.Navigate(pageType);
previousNavigationViewItemSelected = selectedItem;
}
else
{
sender.SelectedItem = previousNavigationViewItemSelected;
}
}
NavigationViewItem.Tag contains the target page. When it is NOT null, we navigate to the target page, and cache the current NavigationViewItem. When it's null, we set the previous cached NavigationViewItem to be the NavigationView.SelectedItem. We expect this will restore the SelectionIndicator to the cached NavigationViewItem, but it didn't. The SelectionIndicator still changed to the clicked NavigationViewItem. Anything wrong here?