如何:在导航历史记录中前进或后退

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

示例

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

在向前导航一个条目之前,必须先通过检查“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

CanGoForward、GoForward、CanGoBack 和 GoBack 由 NavigationWindowFrameNavigationService 实现

注意

如果调用“GoForward”,并且前进导航历史记录中没有条目,或者如果调用“GoBack”,并且后退导航历史记录中没有条目,则会引发 InvalidOperationException