Receive a message without creating a method?

Jul 27, 2011 at 7:10 PM

With Simple MVVM Toolkit is it possible to receive a message without creating a method?

Using MVVM Light in my ViewModels I was able to receive a reference to an object, for example, without creating a one line method that would assign this reference to a local field:

Messenger.Default.Register<MyObject>(this, mo => { localField = mo; });

As far as I can tell with Simple MVVM Toolkit this is not possible... I have to create a method instead:


RegisterToReceiveMessages<MyObject>(MessageTokens.MyObject, OnObjectReceived);


private void OnObjectReceived(object sender, NotificationEventArgs<MyObject> e)
{
    localField = e.Data
}

Thanks.

Coordinator
Jul 31, 2011 at 11:00 PM

Yes, it's pretty much the same in both toolkits.  You can either use a stand-alone method or a lambda expression (which is the same as an anonymous method). Here is the syntax for SImple MVVM:

RegisterToReceiveMessages<MyObject>(MessageTokens.MyObject, (s, ea) => localField = ea.Data);

Remember, you can take any standalone method and convert it to a lambda expression (although it may not always be recommended).

Cheers,

Tony

Aug 4, 2011 at 6:29 PM

Thanks Tony.