In WPF, you have two ways (out of the box) for "showing"/"closing" views. The first, is simply, by showing a Window, or Dialog via the .Show()
or .ShowDialog()
methods, and they can be closed with the .Close()
method. In this case, you can use the MVVMLight Messenger
class as you mentioned to send the show/close messages to the view in a decoupled way. here's an example with "closing".
In the ViewModel:
CloseTheView()
{
Messenger.Default.Send(new CloseTheViewMessage);
}
in the code-behind of your view:
//Constructor
public TheView()
{
...
Messenger.Default.Register<CloseTheViewMessage>( () => this.Close() );
}
as you can see, this involves some code in the code-behind file, but it's no big deal, since it's just one line of functionality.
The second approach is to use the Navigation Framework (which is available for both WPF and Silverlight). You define a "Shell" which is the main Window
(or UserControl
), you put a frame
inside of it, and you make your other views inherit from Page
, and then initiate the navigation from your ViewModel using the instance of the NavigationService
associated with Frame
(or directly the one associated with the page itself).
Hope this helps :)