NavigationService.FragmentNavigation 事件

定义

在开始导航到内容片段时发生,如果所需片段位于当前内容中,或者在加载源 XAML 内容之后(如果所需片段位于不同内容中),则立即发生。

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 

事件类型

示例

以下示例演示如何处理 FragmentNavigation 以提供自定义片段导航行为。 在这种情况下,如果找不到源 XAML 页面中的片段,则示例将打开一个错误 XAML 页。

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

注解

默认情况下,内容片段是名为 UIElement的内容,该内容是 UIElement 设置其属性的 Name ,例如:

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

通过提供具有以下格式的后缀的 URI,导航到 XAML 片段:

#FragmentName

下面显示了引用内容片段的 URI 的示例:

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

在) 引发事件后 LoadCompleted ,源页面加载 (后,片段导航开始,并 NavigationService 尝试查找 XAML 片段。 如果找到 XAML 片段, NavigationService 则指示内容导航器 (NavigationWindowFrame) 显示片段。 如果需要更改此行为,可以处理 FragmentNavigation 以提供自己的片段导航行为。 FragmentNavigation 传递参数, FragmentNavigationEventArgs 该参数公开用于此目的的属性,包括:

可以通过自己的自定义实现处理 FragmentNavigation 来替代默认 WPF 片段实现。 如果这样做,则需要将 设置为 Handledtrue;否则,将应用默认的 WPF 片段处理行为。

应避免从事件处理程序内 FragmentNavigation 直接启动导航。 由于 FragmentNavigation 在现有导航期间引发 ,因此从 FragmentNavigation 事件处理程序启动新导航会创建一个嵌套导航,从而导致 ExecutionEngineException 引发 。 相反,可以通过使用 Dispatcher创建异步工作项来间接启动导航。

注意

当引发 FragmentNavigationNavigationService,它还会Application.FragmentNavigation引发 对象上的 Application 事件。

重要

松散 XAML 页面不支持片段导航, (仅标记 XAML 文件, Page 在以下情况下用作根元素) :

• 导航到松散 XAML 页面中的片段时。

• 从松散 XAML 页面导航到另一个松散 XAML 页中的片段时。

但是,松散 XAML 页可以导航到自己的片段。

适用于

另请参阅