wpf - WPF Modern UI 的 '#1' 中的 '/Content/LoremIpsum.xaml#1' 是什么意思?

标签 wpf xaml modern-ui

我正在调查 ModernUI这些天来,我在修改代码时遇到了一些问题。问题来自 TabControl .来自 MUI DOC 的样本就好像:

<Grid Style="{StaticResource ContentRoot}">
  <mui:ModernTab SelectedSource="/Content/LoremIpsum.xaml#1" Layout="List">
    <mui:ModernTab.Links>
        <mui:Link DisplayName="Lorem Ipsum 1" Source="/Content/LoremIpsum.xaml#1" />
        <mui:Link DisplayName="Lorem Ipsum 2" Source="/Content/LoremIpsum.xaml#2" />
    </mui:ModernTab.Links>
  </mui:ModernTab>
</Grid>

谁能向我解释 #1 的用法?在上面的代码中?

最佳答案

在这种情况下使用片段导航。这意味着当您在 ViewModel 中使用选定的源绑定(bind)时,您可以简单地解析出 # 之后的所有内容找到您的index选择的选项卡。您必须收听 SourceChanged 类型的事件,以了解用户选择了哪个选项卡或使用 OnFragmentNavigation事件。

为此使用了以下代码:

namespace FirstFloor.ModernUI.Windows.Navigation
FragmentNavigationEventArgs.cs

/// <summary>
/// Provides data for fragment navigation events.
/// </summary>
public class FragmentNavigationEventArgs
    : EventArgs
{
    /// <summary>
    /// Gets the uniform resource identifier (URI) fragment.
    /// </summary>
    public string Fragment { get; internal set; }
}

namespace FirstFloor.ModernUI.Windows
IContent.cs
/// <summary>
/// Defines the optional contract for content loaded in a ModernFrame.
/// </summary>
public interface IContent
{
    /// <summary>
    /// Called when navigation to a content fragment begins.
    /// </summary>
    /// <param name="e">An object that contains the navigation data.</param>
    void OnFragmentNavigation(FragmentNavigationEventArgs e);
    ...
}

namespace FirstFloor.ModernUI.Windows.Navigation
NavigationHelper.cs
/// <summary>
/// Removes the fragment from specified uri and return it.
/// </summary>
/// <param name="uri">The uri</param>
/// <returns>The uri without the fragment, or the uri itself if no fragment is found</returns>
public static Uri RemoveFragment(Uri uri)
{
    string fragment;
    return RemoveFragment(uri, out fragment);
}

/// <summary>
/// Removes the fragment from specified uri and returns the uri without the fragment and the fragment itself.
/// </summary>
/// <param name="uri">The uri.</param>
/// <param name="fragment">The fragment, null if no fragment found</param>
/// <returns>The uri without the fragment, or the uri itself if no fragment is found</returns>
public static Uri RemoveFragment(Uri uri, out string fragment)
{
    fragment = null;

    if (uri != null) {
        var value = uri.OriginalString;

        var i = value.IndexOf('#');
        if (i != -1) {
            fragment = value.Substring(i + 1);
            uri = new Uri(value.Substring(0, i), uri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative);
        }
    }

    return uri;
}

您还可以看到一个示例,它与 IContent 一起使用导航。这个问题的界面:

Caliburn.Micro + MEF + Modern UI: IContent events

关于wpf - WPF Modern UI 的 '#1' 中的 '/Content/LoremIpsum.xaml#1' 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22137628/

相关文章:

c# - 由于文本框失去焦点,需要单击两次按钮

c# - MouseEnter WPF 上的发光效果

c# - 如何在 Wpf-app 中使用 9-patch 图像

wpf - 列出WPF特定窗口的所有控件

c# - DatagridTemplateColumn 中的动态

c# - 一个按钮执行两个命令

c# - 如何在wpf中聚焦选项卡

wpf - WPF中的分段文本框

xaml - 忽略绑定(bind)初始化

c# - WPF 现代 UI 中的页面导航