NavigationService.GetNavigationService(DependencyObject) Method

Definition

Gets a reference to the NavigationService for the navigator whose content contains the specified DependencyObject.

C#
public static System.Windows.Navigation.NavigationService GetNavigationService(System.Windows.DependencyObject dependencyObject);

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.

C#
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
C#
    }
}

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

Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10