Export (0) Print
Expand All

How to: Navigate Forward or Back Through Navigation History

This example illustrates how to navigate forward or back to entries in navigation history.

Code that runs from content in the following hosts can navigate forward or back through navigation history, one entry at a time.

Before you can navigate forward one entry, you must first check that there are entries in forward navigation history by inspecting the CanGoForward property. To navigate forward one entry, you call the GoForward method. This is illustrated in the following example:

void navigateForwardButton_Click(object sender, RoutedEventArgs e)
{
    // Navigate forward one page from this page, if there is an entry 
    // in forward navigation history 
    if (this.NavigationService.CanGoForward)
    {
        this.NavigationService.GoForward();
    }
    else
    {
        MessageBox.Show("No entries in forward navigation history.");
    }
}

Before you can navigate back one entry, you must first check that there are entries in back navigation history by inspecting the CanGoBack property. To navigate back one entry, you call the GoBack method. This is illustrated in the following example:

void navigateBackButton_Click(object sender, RoutedEventArgs e)
{
    // Navigate back one page from this page, if there is an entry 
    // in back navigation history 
    if (this.NavigationService.CanGoBack)
    {
        this.NavigationService.GoBack();
    }
    else
    {
        MessageBox.Show("No entries in back navigation history.");
    }
}

CanGoForward, GoForward, CanGoBack, and GoBack are implemented by NavigationWindow, Frame, and NavigationService.

Note Note

If you call GoForward, and there are no entries in forward navigation history, or if you call GoBack, and there are no entries in back navigation history, an InvalidOperationException is thrown.

Show:
© 2014 Microsoft