NavigationService.GetNavigationService(DependencyObject) 方法

定义

获取对导航器的 NavigationService 的引用,该导航器的内容中包含指定的 DependencyObject

public:
 static System::Windows::Navigation::NavigationService ^ GetNavigationService(System::Windows::DependencyObject ^ dependencyObject);
public static System.Windows.Navigation.NavigationService GetNavigationService (System.Windows.DependencyObject dependencyObject);
static member GetNavigationService : System.Windows.DependencyObject -> System.Windows.Navigation.NavigationService
Public Shared Function GetNavigationService (dependencyObject As DependencyObject) As NavigationService

参数

dependencyObject
DependencyObject

由导航器承载的内容中的 DependencyObject

返回

NavigationService

对导航器(该导航器的内容中包含指定的 DependencyObject)的 NavigationService 的引用在某些情况下可以为 null

例外

dependencyObject 参数为 null

示例

以下示例演示如何 UserControl 通过调用 GetNavigationService来检索导航服务。

void getNavigationServiceButton_Click(object sender, RoutedEventArgs e) {
    // Retrieve first navigation service up the content tree
    NavigationService svc = NavigationService.GetNavigationService(this.getNavigationServiceButton);
    if (svc != null)
    {
        // Use navigation service
Private Sub getNavigationServiceButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    ' Retrieve first navigation service up the content tree
    Dim svc As NavigationService = NavigationService.GetNavigationService(Me.getNavigationServiceButton)
    If svc IsNot Nothing Then
        ' Use navigation service
    }
}
    End If
End Sub

注解

导航器具有处理内容导航的导航 NavigationService 。 WPF 有两个导航器: NavigationWindowFrame。 为了处理导航请求和管理导航生存期,导航器使用作为类实现 NavigationService 的 WPF 导航服务。 导航器托管的内容可以通过调用GetNavigationService该方法来获取对导航器的NavigationService引用。

GetNavigationService返回 nulldependencyObject

WPF 提供了两个快捷方式,用于获取对以下内容的 NavigationService引用:

适用于