I’ve just started a new project in which the presentation layer will be done by WPF and MVVM Light by GalaSoft.
I need a lot of views and it’s not clear to me how to manage navigation through windows.
First of all, the templates offered in MVVM Light for creating a new “WPF MVVM View” create a new Window that is not possible to use for navigation by frame (I mean, by putting a frame in mainView and changing the source path to navigate).
Do I simply have to change Window to Page for all the views I create using templates?
Or is there a different way to perform navigation in WPF with the MVVM Light toolkit?
Eventually I did it this way.
Following the idea of o_q, I created NavigationWindow as MainWindow and changed all the the views to page.
Then, I created an inteface and a class which using Navigation:
Then, in viewModelLocator I created all the const string nedded to store the paths to my views:
In App.cs, in the Application_Startup event handler, with the help of Unity IoC I registered a singleton of NavigationService:
Now, in my ViewModelLocator, I can register a “Galasoft” message to catch all the events and navigate to a page; in the constructor I have:
In this way I keep all the viewModels “ignorant”… they don’t know anything about navigation, plus I don’t have code behind.
If I need to navigate by using a button from a view I can resolve NavigationService from the connected viewModel and navigate to the Page I need.
And, most important thing, it works!