WPF and MVVM tutorial 08, messaging.

Before continue our tutorial I want to take a break and talk about the messaging options. Also because the MVVM is a new interpretation of an old pattern, some of my friends are using the Mediator Pattern and today we must talk about that before building the Details view.

As it was requested by many people, here is the location of the source code on CodePlex: www.codeplex.com/MVVM. Please refer there to any issue.

Communication through the views.

Before starting we need to spend 5 minutes over the communication. The first solution we used was the Navigator Pattern. Actually what I do not like of the Navigation Pattern is that we are going to involve one object for the entire applicationÂ’s cycle. A different solution would be the Mediator Pattern.

Over the network there are some pretty cool frameworks like Prism Project, Caliburn Framework and more. For our purpose (understand and implement the MVVM pattern) we need something lighter but better than the simple navigator pattern.

I have found this beta framework that manage the communication between the views in a very nice and clean way: MVVM Light Toolkit.

Using a framework, everything will be easier then. For example, we can implement in our List View a simple command that will pass a selected entity to a Details View in this way:

  1: //Creates detail window
  2: DetalWindow view = new EditDetalWindow();
  3: //Post ShowDetailWindow message
  4: ShowDetailMessage message = new ShowDetailMessage(this, message.Content);
  5: Messenger.Default.Broadcast(message);
  6: //Show Detail Window
  7: view.Show();

MVVM Light Toolkit, implementation with WPF.

This toolkit was created in order to facilitate the implementation of the MVVM pattern inside WPF and Silverlight applications. Actually it has a fancy setup that works great also with Visual Studio 2008. It installs inside VS:

  • MVVM Application Template for Silverlight and WPF
  • ViewModel, View and ViewModelLocator class templates
  • Some cool code snippets

If you want to have a look in depth on how it works, please refer to this page: MVVM light toolkit for WPF and Silverlight.

So, coming back to our project, we just need to locate the .dll and add a reference to our ViewModel layer in this way:


Then we will be able to write some simple code like this one, directly in our existing ViewModels:


Building a Sample Project with Galactic Toolkit.

After the installation you will have these 2 new templates inside Visual Studio:

including some examples.


The difference from what we did until now is in the communication that is handle by messaging. On our solution we were going to use the Navigator in order to send messages and to manipulate objects between the views.

This is done in a different way by Galactic.

First of all the communication with the user.

We still have a static class but it is used in a real different way. You have to create a relay command inside the view model:

  1: public RelayCommand ButtonCommand {
  2:     get;
  3:     private set;
  4: }

Then we need to assign the command, trough the binding process, to our view:

  1: <Button Content="{Binding ButtonContent}"
  2:   Command="{Binding ButtonCommand}"

And finally we can create a Relay Command instance and assign it to our ButtonCommand. Then we use a Message function to talk with the client, in this way:

  1: this.ButtonCommand = new RelayCommand(() =>
  2: {
  3:     var dialogMessage = new DialogMessage(
  4:         this,
  5:         typeof(MainWindow),
  6:         Resources.MessageBoxText,
  7:         r => this.DialogResult = r)
  8:     {
  9:         Button = MessageBoxButton.YesNo,
 10:         Caption = Resources.MessageBoxCaption,
 11:         DefaultResult = MessageBoxResult.No,
 12:         Icon = MessageBoxImage.Question
 13:     };
 15:     Messenger.Default.Broadcast(dialogMessage);
 16: });

Pretty cool right?

If instead of talking with the client, you need to open a new window and pass an entity to it you can do it also through the toolkit.

You can view a simple here on Corrado Cavalli blog.

Now we are able in the next tutorial to write the C.R.U.D. view of our application.

Stay tuned!!

Leave a comment

Your email address will not be published. Required fields are marked *