May 10, 2013 at 6:34 PM
Edited May 10, 2013 at 6:48 PM
Here is an example that I have used. In this example I am using MVVM Foundation Messenger to show a different view.
You will have to do the following in a ViewModel where you want to send the message from. In my example, I am sending a message and passing a parameter (name of another View - it can be any object)
Mediator.NotifyColleagues is a Messenger method that notifies all registered ViewModels (this will be covered shortly). MediatorMessages.ShowInterface is just a class and a string constant that I use for passing the messages around. Much easier then typing
the name every time. The example of this class is below:
public static class MediatorMessages
public const string ShowInterface = "ShowInterface";
Now on to the receiving end. In another ViewModel we have to register the method that will be receiving these messages. Note that in the constructor of your ViewModel you need to register the Messenger:
You decorate the method that will receive the messages as shown below. The definition of this would match the Message you are sending. I am showing the one that matches the example I used:
[MediatorMessageSink(MediatorMessages.ShowInterface, ParameterType = typeof(string))]
private void ShowInterface(string interfaceType)
//The code to handle the Message goes here
For my purposes to show the Dialog I stored the interfaceType into a Parameter (InterfaceName) to which my ShellView is binded to. In the ShellView I use DataTemplates to decide which DialogView to show (based on the InterfaceName). For each Dialog that the
application would show I have the appropriate View and ViewModel. The responses from the Dialog are routed back to the ViewModel that sent the message. Unfortunately I do not currently have an example for this last part, but if you are having issues figuring
things out, I could whip one up.
Hope this helped! Feel free to reply back if you have any questions, I'll try and help if I can.