I have an MVVM application that I'm trying to move over to Modern UI, but I can't seem to figure out the Navigation Service...
Basically, my ViewModel is an ObservableCollection with an "AddItem" method, as an ICommand
. When this method is executed, I want to switch back to the first MUI Group's 1st Link\Page, here is an Example snippet of Modern UI WPF...
<mui:ModernWindow ...>
<mui:ModernWindow.MenuLinkGroups>
<mui:LinkGroup DisplayName="Open Work">
<mui:LinkGroup.Links>
<mui:Link DisplayName="Current Activity" Source="/Pages/ActivityPage.xaml" />
<mui:Link DisplayName="Activity Queue" Source="/Pages/Queue.xaml" />
</mui:LinkGroup.Links>
</mui:LinkGroup>
<mui:LinkGroup DisplayName="Search" >
<mui:LinkGroup.Links>
<mui:Link DisplayName="Customers" Source="/Pages/SearchPage.xaml" />
</mui:LinkGroup.Links>
</mui:LinkGroup>
</mui:ModernWindow.MenuLinkGroups>
So basically, from "SearchPage.xaml", the ICommand "AddItem" is executed, and I want to immediately switch back to "ActivityPage.xaml" (in the "Open Work" group).
Ideally, I'd like to do this from the ViewModel, but I don't mind attaching a Handler to the CollectionChanged
event, and manually setting the Page, but the NavigationService samples (here) haven't helped me figure this one out...
As a last resort, I added MVVM Light from Nuget and I tried implementing IModernNavigationService in my ViewModel, but I can't figure out how to get the ViewModelLocator to work.
Found the answer. Much simpler than I thought it would be.
In my ModernWindow code-behind, I add the following handler to the CollectionChanged
event:
private void Items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if(e.NewItems.Count > 0)
{
try
{
ContentSource = MenuLinkGroups.First().Links.First().Source;
}
catch (Exception error)
{
ModernDialog.ShowMessage(error.Message, FirstFloor.ModernUI.Resources.NavigationFailed, MessageBoxButton.OK);
}
}
}