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.

image 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:

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:

image

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

image

Building a Sample Project with Galactic Toolkit.

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

imageincluding some examples.

image

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:

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

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:

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!!

Tags: