现代 ui wpf 导航 [英] modern ui wpf navigation
问题描述
我正在使用现代 ui wpf 并尝试从 CheckLogin.xaml 页面导航到 MainWindow.xaml 页面(它们位于解决方案根目录中).我从 CheckLogin.xaml 内部写了这个:
I'm using modern ui wpf and trying to navigate from CheckLogin.xaml page to MainWindow.xaml page (they are in solution root directory). From inside CheckLogin.xaml I wrote this:
BBCodeBlock bbBlock = new BBCodeBlock();
bbBlock.LinkNavigator.Navigate(new Uri(url, UriKind.Relative), this);
我为 url 使用了以下值:/MainWindow.xaml"、pack://application:/MainWindow.xaml"、
I used the following values for url: "/MainWindow.xaml", "pack://application:/MainWindow.xaml",
但抛出异常无法导航到 pack://application:/MainWindow.xaml,找不到 ModernFrame 目标 ''".
but an exception thrown "Unable to navigate to pack://application:/MainWindow.xaml, could not find a ModernFrame target ''".
我缺少什么,以及如何正确导航?
what I'm missing, and how to navigate correctly?
推荐答案
使用 NavigationService
使用导航服务在页面之间导航
To use navigation service to navigate between pages
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.或者,您可以通过一些辅助函数找到合适的父级.
these both approach will achieve the navigation too. above sample will only work when you are using them from the child of NavigationWindow i.e. CheckLogin.xaml in this case. alternatively you may find the appropriate parent by some helper functions.
例如.
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
您可能需要指定框架目标
you may need to specify the frame target
string url = "/MainWindow.xaml";
BBCodeBlock bbBlock = new BBCodeBlock();
bbBlock.LinkNavigator.Navigate(new Uri(url, UriKind.Relative), this, NavigationHelper.FrameSelf);
可以为框架目标指定以下选项
following options can be specified for frame target
//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";
这篇关于现代 ui wpf 导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!