ViewModel doesn't receive message in MVVM Light

I have two ViewModels: MainViewModel and QuestionViewModel. I Register they in ViewModelLocator.


On MainViewModel I have a ListBox with Questions. When Click I execute this command

NavigationService.NavigateTo(new Uri("/Pages/QuestionPage.xaml", UriKind.Relative));
Messenger.Default.Send<Question, QuestionViewModel>(q);

QuestionPage's DataContext set to QuestionViewModel. On QuestionViewModel I register message:

Messenger.Default.Register<Question>(this, q =>

But this function work only on second open of QuestionViewModel, because QuestionViewModel instance creates only on first opening. My Question is: Where and When I need to create instance of QuestionViewModel in MVVM Light? I dont want create all my pages at startup.

Now, I create instance in constructor ViewModelLocator:

public ViewModelLocator()

It is good practice?


You're right, this is not a good practice to create all view models in ViewModelLocator constructor. The reason of the issue you got is that QuestionViewModel is created after you send the message. You can try to create a service which will share current Question object and inject this service into MainViewModel and QuestionViewModel.

public interface IQuestionService
    Question CurrentQuestion {get; set;}

public class QuestionService : IQuestionService
    public Question CurrentQuestion {get; set;}

Then on Click in MainViewModel just save current question:

_questionService.CurrentQuestion = q;

and use in in your QuestionViewModel

Just make sure you inject the same instance of IQuestionService into your view models.

One more variant is to pass simple navigation arguments in a URL like this:

NavigationService.NavigateTo(new Uri("/Pages/QuestionPage.xaml?questionid=" + q.Id, UriKind.Relative));

Alternatively, you could try to implement your own NavigationService which supports passing parameters, but this is more complicated.

Need Your Help

c++ const member function

c++ function const member

I am reading a book called 'Effective C++, Second Edition' and its talking about const member functions and how you have bitwise const-ness and conceptual const-ness.

Not able to get the value out of query

php mysql sql

I am running this below code but it returns an error as: "undefined index: value"