在这种情况下使用片段导航。这意味着当您在 中使用选定的源绑定时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
在此问题中看到一个将导航与界面一起使用的示例:
Caliburn.Micro + MEF + 现代 UI:IContent 事件