Compartilhar via


Como navegar para frente ou voltar pelo histórico de navegação

Este exemplo ilustra como navegar para frente ou voltar para entradas no histórico de navegação.

Exemplo

O código executado a partir do conteúdo nos hosts a seguir pode navegar para frente ou voltar pelo histórico de navegação, uma entrada por vez.

Antes de poder navegar para frente em uma entrada, primeiro você deve verificar se há entradas no histórico de navegação avançada inspecionando a propriedade CanGoForward . Para navegar para frente em uma entrada, você chama o método GoForward . Isso é ilustrado no exemplo a seguir:

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

Antes de poder navegar de volta em uma entrada, primeiro você deve verificar se há entradas no histórico de navegação de volta inspecionando a propriedade CanGoBack . Para navegar de volta em uma entrada, você chama o método GoBack . Isso é ilustrado no exemplo a seguir:

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 e GoBack são implementados por NavigationWindow, Framee NavigationService.

Observação

Se você chamar o GoForward e não houver entradas no histórico de navegação para frente ou se você chamar o GoBack e não houver entradas no histórico de navegação de volta, uma InvalidOperationException será lançada.