Messaging in Silverlight with MVVM Light

July 24, 2010 5 Comments

[Update 07/25]

There are two important things to point out on this post (thanks to the guidance from the MVVM Light toolkit author Laurent).  First, if you are solely using the messsaging component and not using the DispatcherHelper, then there is no need to Initialize it.  The second is that your message class does not need to inherit from the MessageBase class.  You can, in fact, send any class thru the messaging system.

These two changes simplify this post quite a bit.  Which, in my opinion, only validates the power and flexibility of the toolkit messaging.

[/Update 07/25]

Being an avid Silverlight developer, I love the MVVM pattern and the benefits of using it.  That being the case, I have used several different frameworks to help with the MVVM plumbing.  However, I keep coming back to my favorite, GalaSoft’s MVVM Light Toolkit.  This toolkit does a great job of handling a lot of the MVVM plumbing code for you, making it much easier to get your applications up and running using the MVVM pattern.

There are many features of the framework that I could talk about, but I wanted to focus on the messaging component.  In decoupled applications, like those using MVVM, handling messaging between components is extremely important.  In fact, you will find a good messaging component a great help in almost any application, decoupled or not.

The messaging part of the MVVM Light toolkit works so well, that I have used it in several non-MVVM applications and would recommend the toolkit on the messaging alone.

So now that you have heard me say that messaging in the MVVM Light toolkit is cool, let’s look into how to use it within our applications.  Since I find it so useful, we are simply going to focus on it and save the rest of the MVVM framework for a different discussion.

Before we get started, it’s important to note that the MVVM Light toolkit can be used for Silverlight, WP7, and WPF applications.  This makes it very handy to build reusable components to use in different scenarios.

To incorporate the toolkit into your application, you need to include the appropriate resource (depending on the language and the version).  Next we need to initialize the toolkit within the application.  This is usually done in the App constructor.

   1: DispatcherHelper.Initialize();

One of the nice things about this messaging system is that you can create your own custom message classes by inheriting the MessageBase class.  The MessageBase class exposes 2 properties: Sender and Target.
 
Let’s start off by building a generic class that inherits from the MessageBase class and stores some information within it.
 
   1: public class CoreMessage : MessageBase

   2: {

   3:     public string Message { get; set; }

   4: }

As you can see, we simply added a Message property to include some content to send with our message.

The next thing you need to do is to register your receiver to accept the new messages.  This is basically attaching to an event handler for the specific messages you wish to receive.

   1: public class Receiver

   2:  {

   3:      public Receiver()

   4:      {

   5:          Messenger.Default.Register<CoreMessage>(this, (action) => ReceiveMessage(action));

   6:      }

   7:  

   8:      private void ReceiveMessage(CoreMessage msg)

   9:      {

  10:         // Message handling code

  11:      }

  12:  }

In the class constructor, we register our class with the toolkit to receive any messages of type CoreMessage.  These messages are then sent to the ReceiveMessage method within the class.  That is all that is required on the receiver end of things.

The last part of the equation is to actually send the messages.  Here is a simple Sender class that demonstrates the message sending process.

   1: public class Sender

   2: {

   3:     public void SendMessage(string message)

   4:     {

   5:         CoreMessage msg = new CoreMessage() { Message = message };

   6:         Messenger.Default.Send<CoreMessage>(msg);

   7:     }

   8: }

Here our SendMessage method accepts a string that is added to a CoreMessage instance.  The message is then sent and received by anyone registered to receive CoreMessage type messages (in our case the Receiver class).

As with all demos, this is a fairly simplified usage of the messaging component.  However, I hope that the potentials and ease of use will spark some ideas.

You can find the MVVM Light toolkit at : http://www.galasoft.ch/mvvm/getstarted/

Filed in: MVVM, Silverlight • Tags: , , , ,

Comments (5)

Trackback URL | Comments RSS Feed

  1. Hi,

    Thanks for the kind words and the article. Two comments:

    – you need to initialize the DispatcherHelper class only if you actually use it. DispatcherHelper is a class that helps you to dispatch method calls back from a background thread to the UI thread. In MVVM Light VNext, initializing the class won’t be needed anymore, as i found a better way to do this.

    – Deriving your messages from MessageBase is optional. You can in fact send/receive any type (simple value types, complex reference types) without any constraint.

    Cheers!
    Laurent

  2. Tony Champion says:

    Laurent,

    Thanks for the comments. Now I like the toolkit even better. :) I’ll make the updates to the post.

    Tony

  3. ramesh says:

    Awesome Article. would you please put the working sample code for this Article.

  4. Teresa Burger says:

    Thank you very much for this article (and everything you do). I was using a differant example and had made it MUCH more difficult than it needed to be.

  5. Tony Champion says:

    Thx. Glad it helped.

Leave a Reply