NavigationService.GetNavigationService(DependencyObject) Method

Definition

Gets a reference to the NavigationService for the navigator whose content contains the specified 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

Parameters

dependencyObject
DependencyObject

The DependencyObject in content that is hosted by a navigator.

Returns

A reference to the NavigationService for the navigator whose content contains the specified DependencyObject; can be null in some cases.

Exceptions

The dependencyObject parameter is null.

Examples

The following example shows how a UserControl can retrieve a navigation service by calling 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

Remarks

A navigator has a NavigationService that handles content navigation. WPF has two navigators: NavigationWindow and Frame. To process navigation requests and manage navigation lifetime, a navigator uses the WPF navigation service, which is implemented as the NavigationService class. Content that is hosted by a navigator can get a reference to the navigator's NavigationService by calling the GetNavigationService method.

GetNavigationService returns null when the dependencyObject:

WPF offers two shortcuts for acquiring a reference to the NavigationService:

Applies to