Search code examples
c#uwpmvvm-light

MVVM message not received when sending from another ViewModel


I have created a detail page containing some quiz questions. I need to send the list of questions to the ViewModel. The data in the receiving ViewModel is always emtpy. The debuger doesn't even go into the 'ReceiveMessage' method. Here is my sending part:

        Frame rootFrame = Window.Current.Content as Frame;
        Messenger.Default.Send<List<QuizQuestion>>(this.Questions);
        rootFrame.Navigate(typeof(QuestionOverviewPage));

And here is my receiving viewmodel:

    public QuestionOverviewViewModel(IMessageDialogService dialogService, IRoundRepository rounRepository)
    {
        _dialogService = dialogService;
        _rounRepository = rounRepository;
        _quizQuestionsViewModels = new List<QuestionViewModel>();
        Messenger.Default.Register<List<QuizQuestion>>
        (
            this,ReceiveMessage
        );
    }

    private void ReceiveMessage(List<QuizQuestion> action)
    {
        Console.WriteLine(action);
    }

Solution

  • MVVM message not received when sending from another ViewModel

    Messenger.Default.Register should be invoked before sending, this step will pass the subscriber to sub-list and send client will find the subscriber instance then call action method with parameter.

    I found you place the Messenger.Default.Register in the QuestionOverviewViewModel, and the Send method called before rootFrame.Navigate, So you could try to call send method after rootFrame.Navigate.

    Frame rootFrame = Window.Current.Content as Frame;       
    rootFrame.Navigate(typeof(QuestionOverviewPage));
    Messenger.Default.Send<List<QuizQuestion>>(this.Questions);