Question

I've been attempting to get to grips with MVVM and am now at the stage of trying to use a messenger. The framework I chose was Josh Smith's MVVM Foundation, mainly because it's small and I'm fairly new to both C# and the MVVM pattern. So far, implementing a message that triggers a method in a different ViewModel has gone smoothly (I think) e.g. to send a message identified by "NameOfMessage" :-

App.Messenger.NotifyColleagues("NameOfMessage");

and on the receiving end e.g. a different ViewModel :-

App.Messenger.Register("NameOfMessage", DoSomething);

Void DoSomething()
{
// Your code that "does something" here.
}

This is all very well and works beautifully; however, it would be great to pass some information along with the message. At the moment it's like sending a postcard to everyone but forgetting to write how amazing your holiday is.

To this end I've tried the following, a property that when set, sends a message under the heading "SentMessage" with the contents of the property SentMessage aka, "_sentMessage" :-

 public string SentMessage
    {
        get { return _sentMessage; }
        set
        {
            _sentMessage = value;
            RaisePropertyChanged("SentMessage");
            App.Messenger.NotifyColleagues("SentMessage", _sentMessage);
        }
    }

This flags no errors and appears to be fine, my problem is the syntax for subscribing to this message :-

App.Messenger.Register("SentMessage", param => { this.ReceivedMessage = (string)param; });

I'd like to receive the message and put the string in another property e.g. ReceivedMessage. What I've tried is above (influenced by Josh's Mediator Prototype) but this raises an error stating "Delegate 'System.Action' does not take 1 arguaments. I guess this is because it's expecting to be pointed to a Method?

If anyone can aid in the correct syntax the help would be greatly appreciated. (This is all written in C# using WPF, .NET Framework 4 and visual studio 2010)

Was it helpful?

Solution

I had used MVVM Foundation for a while before I moved to MVVMLight. I think you have to use below syntax to register callback method for a message. (I haven't tested it, just recall from my memory in the old day.)

App.Messenger.Register("SentMessage", (Action<String>)(param => { this.ReceivedMessage = (string)param; }));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top