Receive a message without creating a method?

Jul 27, 2011 at 8: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


Aug 1, 2011 at 12:00 AM

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).



Aug 4, 2011 at 7:29 PM

Thanks Tony.