As a starter in this all architecture of MVVM I'm having a few doubts regarding the navigation between one window to another. I'm using the Framework MVVM Light.
The behavior I expect is in WinForms like this:
GeneralWindow gw = new GeneralWindow(); this.Hide(); // or close gw.Show();
I already lose a couple of hours trying to find some hints using the messenger, but the methods I found I have to use code-behind in the view and that is not very MVVMish.
Best regards and thank you in advance.
The behavior I expect is in WinForms like this:
MVVM pattern divides
View
fromViewModel
. So it is not eligible to create newView
fromViewModel
. Creating window instance and showing window from view model is violation of MVVM". So I suggest you to use the popular technique where you can changeViews
usingContentControl
andDataTemplate
.Let's dive in this technique:
If
Window.DataContext
is an instance ofViewModelA
, thenViewA
will be displayed andWindow.DataContext
is an instance ofViewModelB
, thenViewB
will be displayed.Let me show an example where it can be seen where you should put
DataTemplates
:The best example I've ever seen and read it is made by Rachel Lim. See the example.
Update:
If you want really open new Window, then you should create an intermediate layer to make ViewModel not dependent on a concrete implementation of creating new window.
How to close a Window?
There are a lot of approaches.. One of them is: