ViewModelBase.MessengerInstance Null

Apr 2, 2010 at 5:21 PM

I tried using the MessengerInstance property from ViewModelBase and got a null reference exception. I had assumed that it would use the default messenger if none was set with the ViewModelBase's ctor overload.

Looking at the source code, the comments on MessengerInstance states "If null, this class will attempt to broadcast using the Messenger's default instance." So it looks like my assumptions were what the code was originally intended to do, but it was never written that way.

Also, as an aside, I can tell that lots of time and effort went into documenting the code through XML comments, but none of it comes through intellisense in the latest release because xml files weren't included in the zip files.

Apr 2, 2010 at 7:57 PM

Dude, you rock.

1) MessengerInstance is the instance used to send messages when the method ViewModelBase.RaisePropertyChanged<T>(string propertyName, T oldValue, T newValue, bool broadcast);

Calling this method causes a PropertyChangedMessage<T> to be sent in addition to the PropertyChanged event being raised. To send this message, if an instance of Messenger was passed to the constructor, I use that one. Otherwise, I use Messenger.Default.

In the current implementation, MessengerInstance is null if no instance of messenger was passed to the ViewModel's constructor. Your idea makes much more sense, and I will modify the code in V4 to rather return Messenger.Default instead of null.

2) XML files: Yeah, totally. I simply and completely forgot to include them. I am about to publish a service pack including improvement for the Windows Phone 7, it is a great occasion to correct this mistake.

Thanks so much for the great feedback.


Mar 30, 2011 at 12:49 PM

I added a custom base class below you viewmodelbase in order to match the excepted behavior ;)

public class ViewModelBase : GalaSoft.MvvmLight.ViewModelBase

        protected new IMessenger MessengerInstance
            get { return base.MessengerInstance ?? Messenger.Default; }
            set { base.MessengerInstance = value; } 

Mar 30, 2011 at 12:56 PM

This has been corrected in the new bits. They are available as source only for the moment, but you can follow [url:] and build your own DLLs.