ViewModel doesn't receive message in MVVM Light

£可爱£侵袭症+ 提交于 2019-12-07 14:10:28

问题


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.



来源:https://stackoverflow.com/questions/11485897/viewmodel-doesnt-receive-message-in-mvvm-light

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!