Bewerken

Delen via


NavigationService.FragmentNavigation Event

Definition

Occurs when navigation to a content fragment begins, which occurs immediately, if the desired fragment is in the current content, or after the source XAML content has been loaded, if the desired fragment is in different content.

public:
 event System::Windows::Navigation::FragmentNavigationEventHandler ^ FragmentNavigation;
public event System.Windows.Navigation.FragmentNavigationEventHandler FragmentNavigation;
member this.FragmentNavigation : System.Windows.Navigation.FragmentNavigationEventHandler 
Public Custom Event FragmentNavigation As FragmentNavigationEventHandler 

Event Type

Examples

The following example shows how to handle FragmentNavigation to provide custom fragment navigation behavior. In this case, the example opens an error XAML page if the fragment in the source XAML page is not found.

void NavigationService_FragmentNavigation(object sender, FragmentNavigationEventArgs e)
{
    // Get content the ContentControl that contains the XAML page that was navigated to
    object content = ((ContentControl)e.Navigator).Content;

    // Find the fragment, which is the FrameworkElement with its Name attribute set
    FrameworkElement fragmentElement = LogicalTreeHelper.FindLogicalNode((DependencyObject)content, e.Fragment) as FrameworkElement;

    // If fragment found, bring it into view, or open an error page
    if (fragmentElement == null)
    {
        this.NavigationService.Navigate(new FragmentNotFoundPage());

        // Don't let NavigationService handle this event, since we just did
        e.Handled = true;
    }
}
Private Sub NavigationService_FragmentNavigation(ByVal sender As Object, ByVal e As FragmentNavigationEventArgs)
    ' Get content the ContentControl that contains the XAML page that was navigated to
    Dim content As Object = (CType(e.Navigator, ContentControl)).Content

    ' Find the fragment, which is the FrameworkElement with its Name attribute set
    Dim fragmentElement As FrameworkElement = TryCast(LogicalTreeHelper.FindLogicalNode(CType(content, DependencyObject), e.Fragment), FrameworkElement)

    ' If fragment found, bring it into view, or open an error page
    If fragmentElement Is Nothing Then
        Me.NavigationService.Navigate(New FragmentNotFoundPage())

        ' Don't let NavigationService handle this event, since we just did
        e.Handled = True
    End If
End Sub

Remarks

By default, a content fragment is content that is contained by a named UIElement, which is a UIElement whose Name attribute is set, eg:

<TextBlock Name="FragmentName">...</TextBlock>

You navigate to a XAML fragment by providing a URI with a suffix in the following format:

#FragmentName

The following shows an example of a URI that refers to a content fragment:

http://www.microsoft.com/targetpage.xaml#FragmentName

After the source page loads (after LoadCompleted event is raised), fragment navigation begins and the NavigationService attempts to locate the XAML fragment. If the XAML fragment is found, NavigationService instructs the content navigator (NavigationWindow, Frame) to show the fragment. If you need to change this behavior, you can handle FragmentNavigation to provide your own fragment navigation behavior. FragmentNavigation is passed a FragmentNavigationEventArgs parameter which exposes properties that are useful for this purpose, including:

You can handle FragmentNavigation to override the default WPF fragment implementation with your own custom implementation. If you do so, you need to set Handled to true; otherwise, the default WPF fragment processing behavior is applied.

You should avoid directly initiating navigation from within a FragmentNavigation event handler. Since FragmentNavigation is raised during an existing navigation, initiating a new navigation from a FragmentNavigation event handler creates a nested navigation that can cause the ExecutionEngineException to be thrown. Instead, you can indirectly initiate navigation by creating an asynchronous work item using the Dispatcher.

Note

When NavigationService raises FragmentNavigation, it also raises Application.FragmentNavigation event on the Application object.

Important

Fragment navigation is not supported for loose XAML pages (markup-only XAML files with Page as the root element) in the following cases:

• When navigating to a fragment in a loose XAML page.

• When navigating from a loose XAML page to a fragment in another loose XAML page.

However, a loose XAML page can navigate to its own fragments.

Applies to

See also