Compartir vía


Cómo: Navegar hacia delante o hacia atrás por el historial de navegación

En este ejemplo se muestra cómo navegar hacia adelante o volver a las entradas del historial de navegación.

Ejemplo

El código que se ejecuta desde el contenido de los hosts siguientes puede navegar hacia delante o hacia atrás por el historial de navegación, una entrada a la vez.

Para poder avanzar una entrada, primero debe comprobar si hay entradas en el historial de navegación hacia delante inspeccionando la propiedad CanGoForward. Para avanzar una entrada, llame al método GoForward. Esto se ilustra en el ejemplo siguiente:

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

Para poder retroceder una entrada, primero debe comprobar si hay entradas en el historial de navegación hacia atrás inspeccionando la propiedad CanGoBack. Para retroceder una entrada, llame al método GoBack. Esto se ilustra en el ejemplo siguiente:

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 y GoBack se implementan mediante NavigationWindow, Frame y NavigationService.

Nota:

Si llama a GoForward y no hay entradas en el historial de navegación hacia delante, o si llama a GoBack y no hay entradas en el historial de navegación hacia atrás, se produce una excepción InvalidOperationException.