wpfmvvmmessaging

One of MVVM ViewModels does not receive messages


In one MainViewModel I am sending a message:

private void TextBoxKeyDownVMMethod(KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        MessengerInstance.Send<Messages.WebTab.NavigatingToPageArgs>(new NavigatingToPageArgs { UrlForNavigating = "http://stackoverflow.com/questions/11485897/viewmodel-doesnt-receive-message-in-mvvm-light" });
        //GoToPageInSelectedTabVMCommand.Execute((object)null);
        Keyboard.ClearFocus();
    }
} 

In another one I am receiving it:

public WebTabItemVievModel()
{       
    MessengerInstance.Register<NotificationMessage<Messages.WebTab.NavigatingToPageArgs>>(this, _MessageHandler_NavigatingToPage);
}

private void _MessageHandler_NavigatingToPage(NotificationMessage<NavigatingToPageArgs> args)
{
    GoToPageCommand.Execute(args.Content.UrlForNavigating);
}

WebTabItemVievModel is created as ObservableCollection in constructor of MainViewModel, this collection is ItemsSource for tab control

public MainViewModel()
        {            
            WebTabItems = new ObservableCollection<WebTabItemVievModel>();            
            WebTabItemVievModel tabItem; 
            tabItem = ServiceLocator.Current.GetInstance<WebTabItemVievModel>();
            tabItem.Header = "Empty tab";
            WebTabItems.Add(tabItem);
            tabItem = new WebTabItemVievModel { Header = "Empty tab" };
            WebTabItems.Add(tabItem);
            tabItem = new WebTabItemVievModel { Header = "Empty tab" };
            WebTabItems.Add(tabItem);           
            tabItem = new WebTabItemVievModel() { AddNewTabOnTabHeaderClick =true};
            WebTabItems.Add(tabItem);
}

WebTabItemVievModel has its own UserControl for visualisation, which is binded through resource definition in MainWindow.xaml:

<Window.Resources>
    <DataTemplate DataType="{x:Type vm:WebTabItemVievModel}"  >
        <v:WebTabItemContentViev></v:WebTabItemContentViev>
    </DataTemplate>
</Window.Resources>

Locator already has true parameter while registering. It did not change anything:

    SimpleIoc.Default.Register<WebTabItemVievModel>(true);
    SimpleIoc.Default.Register<MainViewModel>(true);

Who has any ideas why WebTabItemVievModels do not receive messages?


Solution

  • While registering the type is

    NotificationMessage

    But while sending the type is Messages.WebTab.NavigatingToPageArgs

    Make types same and it should work