NavigationService.GetNavigationService(DependencyObject) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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
:
Is a NavigationWindow.
Is a Frame that:
Is hosted by another navigator.
Has its JournalOwnership property set to UsesParentJournal.
Is not part of content that is hosted by a navigator.
WPF offers two shortcuts for acquiring a reference to the NavigationService:
The NavigationService that processed the navigation to a Page can be accessed from the Page itself by getting the value of its Page.NavigationService property.
The NavigationService that is used by a Frame to process navigation can be accessed by getting the value of the NavigationService property.
Applies to
.NET