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

NavigationService

页的宿主用来管理导航的 NavigationService 对象;或者,如果宿主不支持导航,则为 nullThe 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 IsNot 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

页面可由多种类型的主机托管,包括 WindowNavigationWindowFrame和浏览器。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.

相反,它可以尝试使用导航服务,该服务是一项支持浏览器样式导航并由 NavigationService 类封装的服务。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. 不过,你不能创建自己的 NavigationService 实例。You cannot create your own NavigationService instance, though. 相反,主机类型(如 NavigationWindowFrame或浏览器)创建自己的 NavigationService 实例,你可以从 NavigationService 属性访问该实例。Instead, host types such as NavigationWindow, Frame, or a browser create their own NavigationService instance that you can access from the NavigationService property.

NavigationService 属性返回的导航服务是由 visual tree 上的第一个导航器管理 NavigationService 类的实例。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. 如果未找到,则返回 null,指示页的主机不支持导航。If one is not found, null is returned, indicating a page's host does not support navigation.

Note

Window 类不支持导航,也不提供导航服务。The Window class does not support navigation and does not provide a navigation service.

Applies to