Tuesday 16 October 2012

Using MVVM Foundation Messenger to Send Data - Stack Overflow

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)

Source: http://stackoverflow.com/questions/12895172/using-mvvm-foundation-messenger-to-send-data

jr martinez melasma jimmy rollins jimmy rollins let it snow jason trawick jerry lewis

No comments:

Post a Comment