MvvmLight: Cleaning the References of Messenger in Silverlight4


Hi there,
In the recent past i work with Messenger class of MvvmLight sdk. I need to call a view model's method while i was in another screen's viewmodel, i don't think that would be possible but Messenger has did it for me. So where is the problem ?

Problem arises when that page in which you register a method via Messenger's class, loaded twice for example, then Messenger class will not delete the old reference for that method i.e. now your method will be called twice instead of one time. This situation calls for trouble, i searched for a way to remove all the references that belongs to Messenger class on that page loading, i tried many solution as you also find when you search for it but i find the below method working so i though it would be nice to share it with others.


In your viewmodel's constructor register a method as you register any other method, that will clean all the references.

//cleaning up ref of messenger class
                Messenger.Default.Register<string>(this, CommandMessages.CleanUpMessengerRef, false, CleanUp); 

this => current view model
CommandMessages => It is an enum that contains the commands
CleanUp => method that will clean the references i.e. Action that will receive string as a parameter

Below is the method for CleanUp

        /// <summary>
        /// deleting all ref  for messenger class
        /// </summary>
        /// <param name="msg"></param>
        private void CleanUp(string msg)


Popular Posts