편집

다음을 통해 공유


Page.NavigationService Property

Definition

Gets the navigation service that the host of the page is using to manage navigation.

public:
 property System::Windows::Navigation::NavigationService ^ NavigationService { System::Windows::Navigation::NavigationService ^ get(); };
public System.Windows.Navigation.NavigationService NavigationService { get; }
member this.NavigationService : System.Windows.Navigation.NavigationService
Public ReadOnly Property NavigationService As NavigationService

Property Value

The NavigationService object that the host of the page is using to manage navigation, or null if the host does not support navigation.

Examples

The following example shows how a page can check if a navigation service is available and, if so, use it to navigate back to the previous page.

public partial class HomePage : Page
{
    public HomePage()
    {
        InitializeComponent();

        // Don't allow back navigation if no navigation service
        if (this.NavigationService == null)
        {
            this.goBackButton.IsEnabled = false;
        }
    }

    void goBackButton_Click(object sender, RoutedEventArgs e)
    {
        // Go to previous entry in journal back stack
        if (this.NavigationService.CanGoBack)
        {
            this.NavigationService.GoBack();
        }
    }
}
Partial Public Class HomePage
    Inherits Page
    Public Sub New()
        InitializeComponent()

        ' Don't allow back navigation if no navigation service
        If Me.NavigationService Is Nothing Then
            Me.goBackButton.IsEnabled = False
        End If
    End Sub

    Private Sub goBackButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        ' Go to previous entry in journal back stack
        If Me.NavigationService.CanGoBack Then
            Me.NavigationService.GoBack()
        End If
    End Sub
End Class

Remarks

Pages can be hosted by multiple types of hosts, including Window, NavigationWindow, Frame, and a browser.

Pages often need to integrate with their host's navigation to provide in-page navigation support. However, because a page may not know what its host will be at run time, it cannot integrate directly with its host's navigation members to do so.

Instead, it can attempt to use a navigation service, which is a service that supports browser-style navigation and is encapsulated by the NavigationService class. You cannot create your own NavigationService instance, though. Instead, host types such as NavigationWindow, Frame, or a browser create their own NavigationService instance that you can access from the NavigationService property.

The navigation service that is returned from the NavigationService property is the instance of the NavigationService class that is managed by the first navigator up the visual tree. If one is not found, null is returned, indicating a page's host does not support navigation.

Note

The Window class does not support navigation and does not provide a navigation service.

Applies to