这些天我正在研究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
的用法吗?
答案 0 :(得分:4)
在这种情况下使用片段导航。这意味着当您在ViewModel
中使用所选的源代码绑定时,您只需在#
之后解析所有内容,即可找到该选项卡的 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
界面一起使用导航的示例: