This project has moved and is read-only. For the latest updates, please go here.

MVVM Light Messenger Action Executing Multiple Times

Dec 21, 2012 at 3:16 PM
Edited Dec 23, 2012 at 2:31 PM

Thanks for StackOverFlow, I got the first response. I tried and it worked. I did not use attached behavior. I used EventTrigger.


    <!-- In order to Call Cleanup in ViewModel to unregister Messenger.  -->
    <interactivity:EventTrigger EventName="Unloaded">
        <interactivity:InvokeCommandAction Command="{Binding ViewUnloadCommand}" />

Then my view will call the command in ViewModel to unregister the Messenger when this view is unloaded.

Thanks again.


Thanks Laurent for your fantastic work on MVVM light.

I've been working on a WPF project using this framework. Then I encountered this issue. I tried to search it on Google, MSDN and StackOverFlow. I found this solution when Messgener is used between ViewModel and View. I would do something like this in CodeBehind file, to call Unregister in Unloaded event handler.

          public FinishedTodoItemTreeViewUserControl()

             Messenger.Default.Register<DialogMessage>(this, FinishedTodoItemTreeViewModel.DeleteAllDoneItemsConfirmMessageToken, dialog =>
                var confirmResult = MessageBox.Show(dialog.Content, dialog.Caption, dialog.Button, dialog.Icon);

         private void currentControl_Unloaded(object sender, RoutedEventArgs e)

But when I am doing this in ViewModel, when I should call Unregister or Cleanup? Because I still need to receive this message again when it happens. But I don't want to receive this message multiple times with just one shot.

Thanks in advance. 

        /// <summary>
         /// Register to be observer of TodoItems change notification receiver.
         /// </summary>
         private void RegisterTodoItemsChangedNotification()
             Messenger.Default.Register<UnfinishedTodoItemTreeViewModel>(this, UnfinishedTodoItemTreeViewModel.RelatedTodoItemsChangedMessageToken, itemTreeViewModel =>
                if (itemTreeViewModel.ActionCategory == UnfinishedTodoItemTreeViewModel.TodoItemActionCategory.Done)
                    AllTodoItemCount -= 1;
                else if (itemTreeViewModel.ActionCategory == UnfinishedTodoItemTreeViewModel.TodoItemActionCategory.Undone)
                    AllTodoItemCount += 1;