The OnNavigatedTo and OnNavigatedFrom methods allow us to execute code when a Page of a Windows Store app is, respectively, loaded or unloaded. However, if we’re using the MVVM pattern, we shouldn’t use these methods directly.

A possible solution, using MVVM Light Toolkit, is to define an interface that must be implemented by every ViewModel that have to handle the load/unload events:

In the OnNavigatedTo and OnNavigatedFrom methods, we check if the DataContext implements the INavigable interface: in this case, we call its Activate or Deactivate methods, passing the corresponding parameter to them.

Finally, we need to modify our View in order to use BindablePage instead of the standard Page:

And, of course, we need to modify the code-behind too, changing the base class from which MainPage inherits.

If we’re using the LayoutAwarePage class, we can follow the same solution, but is this case we can override the LoadState and SaveState methods, so we can also save and restore state using the ViewModel: