使用导航服务
使用导航服务在页面之间导航
string url = "/Page1.xaml";
NavigationService nav = NavigationService.GetNavigationService(this);
nav.Navigate(new System.Uri(url, UriKind.RelativeOrAbsolute));
替代方法
使用 uri
string url = "/Page1.xaml";
NavigationWindow nav = this.Parent as NavigationWindow;
nav.Navigate(new System.Uri(url, UriKind.RelativeOrAbsolute));
使用对象
NavigationWindow nav = this.Parent as NavigationWindow;
nav.Navigate(new Page1());
这两种方法也将实现导航。上面的示例仅在您从 NavigationWindow 的子级使用它们时才有效,即在这种情况下为 CheckLogin.xaml。或者,您可以通过一些辅助函数找到合适的父级。
例如。
NavigationWindow nav = FindAncestor<NavigationWindow>(this);
public static T FindAncestor<T>(DependencyObject dependencyObject) where T : DependencyObject
{
var parent = VisualTreeHelper.GetParent(dependencyObject);
if (parent == null) return null;
var parentT = parent as T;
return parentT ?? FindAncestor<T>(parent);
}
使用 LinkNavigator
您可能需要指定框架目标
string url = "/MainWindow.xaml";
BBCodeBlock bbBlock = new BBCodeBlock();
bbBlock.LinkNavigator.Navigate(new Uri(url, UriKind.Relative), this, NavigationHelper.FrameSelf);
可以为框架目标指定以下选项
//Identifies the current frame.
public const string FrameSelf = "_self";
//Identifies the top frame.
public const string FrameTop = "_top";
//Identifies the parent of the current frame.
public const string FrameParent = "_parent";