How to: Navigate Forward or Back Through Navigation History

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

Example

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.");
    }
}
Private Sub navigateForwardButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
	' Navigate forward one page from this page, if there is an entry
	' in forward navigation history
	If Me.NavigationService.CanGoForward Then
		Me.NavigationService.GoForward()
	Else
		MessageBox.Show("No entries in forward navigation history.")
	End If
End Sub

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.");
    }
}
Private Sub navigateBackButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
	' Navigate back one page from this page, if there is an entry
	' in back navigation history
	If Me.NavigationService.CanGoBack Then
		Me.NavigationService.GoBack()
	Else
		MessageBox.Show("No entries in back navigation history.")
	End If
End Sub

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

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.