ViewModel doesn't receive message in MVVM Ligh

2019-05-11 23:59发布

问题:

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

SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<QuestionViewModel>();

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 =>
{
    MessageBox.Show("!");
});

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()
{
    ServiceLocator.Current.GetInstance<QuestionViewModel>();
}

It is good practice?

回答1:

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.