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片段实现。 如果这样做,则需要将 Handled 设置为 true;否则,将应用默认WPF片段处理行为。

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

注释

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

Important

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

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

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

适用于

另请参阅