9

Could someone be so kind as to explain MVVM Light's Messenger for me? I was reading a post on StackOverflow here: MVVM pass values between view models trying to get this. The documentation on MVVM Light's not that great at this point so I'm completely unsure where to go.

Say I have two ViewModels and a ViewModelLocator. I want to be able to pass parameters between all three without issue. How would I go about doing this with the messenger? Is it capable of that?

Edit: Here's my new implementation. As of now, it looks as if MessengerInstance doesn't call for a token. I'm terribly confused.

In the first ViewModel:

MessengerInstance.Send<XDocument>(SelectedDocument);

And in the second:

MessengerInstance.Register<XDocument>(this, xdoc => CopySettings(xdoc));

Could be completely wrong. Haven't gotten a chance to test it, but visual studio gets less angry with me when I do it this way. Also the MessengerInstance does register before the Message is sent.

Community
  • 1
  • 1
DanteTheEgregore
  • 1,530
  • 5
  • 23
  • 44

1 Answers1

19

Say I have two ViewModels and a ViewModelLocator. I want to be able to pass parameters between all three without issue. How would I go about doing this with the messenger? Is it capable of that?

That's exactly what it's for, yes.

To send a message:

MessengerInstance.Send(payload, token);

To receive a message:

MessengerInstance.Register<PayloadType>(
    this, token, payload => SomeAction(payload));

There are many overloads, so without knowing exactly what you're trying to accomplish via the messenger, I won't go into all of them, but the above should cover the simple case of wanting to send and receive a message with a payload.

Note that "token" can be really anything that identifies the message. While a string is often used for this, I prefer to use an enum because it's a little safer and enables intellisense, "find usages", etc.

For example:

public enum MessengerToken
{
    BrushChanged,
    WidthChanged,
    HeightChanged
}

Then your send/receive would be something like:

// sending view model
MessengerInstance.Send(Brushes.Red, MessengerToken.BrushChanged);

// receiving view model

// put this line in the constructor
MessengerInstance.Register<Brush>(this, token, brush => ChangeColor(brush));

public void ChangeColor(Brush brush)
{
    Brush = brush;
}

[EDIT] URL to devuxer's comment below changed to: http://blog.galasoft.ch/posts/2009/09/mvvm-light-toolkit-messenger-v2/

Bobby Tait
  • 99
  • 1
  • 8
devuxer
  • 41,681
  • 47
  • 180
  • 292
  • This is perfect! I've tried searching for documentation on this but found none. Do you know of any documentation I might be missing? I've seen almost none for MVVM Light. – DanteTheEgregore Aug 06 '13 at 18:50
  • The guy who developed MVVM Light, Laurent Bugnion, tends to use his blog for introducing new features or updates to features. Here is a specific entry on the messenger you might find helpful: http://blog.galasoft.ch/lbugnion/archive/2009/09/27/mvvm-light-toolkit-messenger-v2-beta.aspx – devuxer Aug 06 '13 at 18:54
  • I've been out for surgery and didn't get a chance to start implementing this till today. I don't see where MessengerInstance calls for a token. I'll add my implementation to the main post. I also posted another question here: http://stackoverflow.com/questions/18241290/mvvm-light-messenger-not-functioning-as-expected/18241995?noredirect=1#18241995 – DanteTheEgregore Aug 14 '13 at 21:22