共用方式為


如何:在巡覽記錄中前移或後移

此範例說明如何向前或向後瀏覽導航歷史中的項目。

範例

從下列主機中的內容執行的程式代碼,可以向前或向後瀏覽導航歷史,一次瀏覽一個項目。

在您可以向前瀏覽一個項目之前,您必須先透過查檢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

在您可以向後瀏覽一個項目之前,您必須先透過查檢CanGoForward屬性,檢查在向後導覽歷史中是否有項目。 若要向後瀏覽一個項目,您可以呼叫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

CanGoForwardGoForwardCanGoBackGoBack是藉由NavigationWindowFrameNavigationService進行實作。

注意

如果您呼叫GoForward,且在向前導覽歷史中沒有任何項目,或者如果您呼叫GoBack,且在向後導覽歷史中沒有任何項目,則會拋出InvalidOperationException