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.
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.
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.
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.
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/