如何根据导航历史记录前进或后退

此示例说明如何在导航历史记录中向前或向后导航条目。

示例:

从以下主机中的内容运行的代码可以通过导航历史向前或向后导航,一次一个条目。

在向前导航一个条目之前,必须先通过检查 CanGoForward 属性来检查前向导航历史记录中是否存在条目。 若要向前导航一个条目,请调用 GoForward 方法。 以下示例对此进行了说明:

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

在导航回一个条目之前,必须先通过检查 CanGoBack 属性来检查后退导航历史记录中是否存在条目。 若要导航回一个条目,请调用 GoBack 方法。 以下示例对此进行了说明:

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

CanGoForwardGoForwardCanGoBackGoBackNavigationWindowFrameNavigationService实现。

注释

如果调用 GoForward,并且前向导航历史记录没有条目,或者调用 GoBack,但后退导航历史记录无条目,则会抛出 InvalidOperationException