次の方法で共有


方法: ナビゲーション履歴を前後に移動する

この例では、ナビゲーション履歴のエントリに進むか戻る方法を示します。

次のホストのコンテンツから実行されるコードは、一度に 1 つのエントリで、ナビゲーション履歴を前後に移動できます。

1 つのエントリを前方に移動する前に、最初に CanGoForward プロパティ 調べることで、前方ナビゲーション履歴にエントリがあることを確認する必要があります。 1 つのエントリを前方に移動するには、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

1 つのエントリに戻る前に、CanGoBack プロパティ 調べることで、戻るナビゲーション履歴にエントリがあることを確認する必要があります。 1 つのエントリに戻るには、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

CanGoForwardGoForwardCanGoBack、および GoBack は、NavigationWindowFrame、および NavigationServiceによって実装されます。

手記

GoForward を呼び出して、ナビゲーション履歴の前方にエントリがない場合、または GoBack を呼び出して、ナビゲーション履歴の後方にエントリがない場合は、InvalidOperationException がスローされます。