现代ui wpf导航 [英] modern ui wpf navigation

查看:181
本文介绍了现代ui wpf导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用现代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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆