If you have several Pages in your application that are children of a Frame then you may want to handle Frame.Navigating and Navigated events in your pages' view models.
Here is a sample root model of your application:
public class RootModel
{
public RootModel()
{
NavigationState = new NavigationState();
}
public NavigationState NavigationState { get; set; }
public bool CanGoBack { get { return NavigationState.CanGoBack; } }
public void GoBack()
{
NavigationState.GoBack();
}
public void GoToHomePage()
{
NavigationState.Navigate(typeof (HomePage));
}
}
Set the global DataContext in the Application derived class of your application:
sealed partial class App : Application
{
...
public RootModel RootModel { get; private set; }
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
RootModel = new RootModel();
var frame = new Frame { DataContext = RootModel };
// Bind the NavigationState and the frame using the ElementBinder class.
// You can also do it in XAML if you need.
ElementBinder.SetWrapper(frame, RootModel.NavigationState);
Window.Current.Content = frame;
Window.Current.Activate();
RootModel.GoToHomePage();
}
}
The home page model that handles navigation events:
public class PdfViewerPageModel : PageModel // Or implement IPageModel.
{
public override void OnNavigated()
{
// TODO Do something here to initialize/update your page.
}
public override void OnNavigating(ref bool cancel)
{
// TODO Do something here to clean up your page.
}
}
The page XAML:
<Page ... DataContext="{Binding HomePageModel}">
<!-- Your page content goes here -->
</Page>