Share via


How to: Navigate Back Through Navigation History

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

Example

Code that is running from content that is hosted in a NavigationWindow, Frame use NavigationService, or Windows Internet Explorer can navigate back through navigation history, one entry at a time.

Navigating back one entry requires first checking that there are entries in back navigation history, by inspecting the CanGoBack property, before navigating back one entry, by calling 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.");
    }
}

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

NoteNote:

If you call GoBack, and there are no entries in back navigation history, an InvalidOperationException is raised.