现代 ui wpf 导航

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24221571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:55:26  来源:igfitidea点击:

modern ui wpf navigation

wpfnavigationmodern-ui

提问by user3222589

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:

我正在使用现代 ui wpf 并尝试从 CheckLogin.xaml 页面导航到 MainWindow.xaml 页面(它们位于解决方案根目录中)。从 CheckLogin.xaml 里面我写了这个:

BBCodeBlock bbBlock = new BBCodeBlock();
bbBlock.LinkNavigator.Navigate(new Uri(url, UriKind.Relative), this);

I used the following values for url: "/MainWindow.xaml", "pack://application:/MainWindow.xaml",

我对 url 使用了以下值:“/MainWindow.xaml”、“pack://application:/MainWindow.xaml”、

but an exception thrown "Unable to navigate to pack://application:/MainWindow.xaml, could not find a ModernFrame target ''".

但抛出异常“无法导航到 pack://application:/MainWindow.xaml,找不到 ModernFrame 目标 ''”。

what I'm missing, and how to navigate correctly?

我缺少什么,以及如何正确导航?

回答by pushpraj

Using 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));

Alternative approach

替代方法

Using uri

使用 uri

    string url = "/Page1.xaml";
    NavigationWindow nav = this.Parent as NavigationWindow;
    nav.Navigate(new System.Uri(url, UriKind.RelativeOrAbsolute));

Using object

使用对象

    NavigationWindow nav = this.Parent as NavigationWindow;
    nav.Navigate(new Page1());

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 的子级(即 CheckLogin.xaml 在这种情况下)使用它们时才有效。或者,您可以通过一些辅助函数找到合适的父级。

Eg.

例如。

    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);
    }

Using 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";