views:

47

answers:

1

Hi All, I am trying to use MVVM light messaging to send a value from one page to another during Navigation (for example, send the id of an item that was selected to an edit page). So the list page's viewmodel sends a message and then sends a navigation message to the view which redirects to the edit page. The edit page's viewmodel gets created only when the navigation to the page happens. So when I register for this event in the edit page viewmodel, I never get the message? What is the best solution for this?

Thanks in advance.

A: 

Your best solution would be to use the querystring instead of messaging. If you don't use the querystring, you'll have to deal with situations like the application being deactivated (tomestoned), then the user clicking "back" and your application loads the second page without receiving the message.

However, if you want to continue down this path, you can modify your ViewModelLocator such that your page's ViewModel is created immediately (in ctor for instance) instead of as needed. Since the ViewModelLocator is created as soon as your App.xaml is loaded, you know that any view models will be created immediately. As long as your view model is registering for messages in it's constructor it should receive the message.

Matt Casto