Compartir a través de


Cómo: Navegar hacia delante o hacia atrás a través del historial de navegación

En este ejemplo se muestra cómo navegar hacia delante 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 a través del historial de navegación, una entrada a la vez.

Para poder navegar hacia delante una entrada, primero debe comprobar que hay entradas en el historial de navegación hacia delante inspeccionando la propiedad CanGoForward . Para navegar hacia delante una entrada, llame al método GoForward . Esto se muestra 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 volver a una entrada, primero debe comprobar que hay entradas en el historial de navegación inversa inspeccionando la propiedad CanGoBack . Para volver a una entrada, llame al método GoBack . Esto se muestra 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, Framey NavigationService.

Nota:

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