【问题标题】:modern ui wpf navigation现代 ui wpf 导航
【发布时间】:2014-08-04 23:20:20
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: wpf navigation modern-ui


    【解决方案1】:

    使用 NavigationService

    使用导航服务在页面之间导航

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

    【讨论】:

    • bbBlock.LinkNavigator.Navigate 没有采用一个参数的重载!您在提交之前尝试过您的解决方案吗?
    • 感谢@pushpraj 回复。我尝试了所有选项并得到了这个异常(无法导航到 /MainWindow.xaml,找不到 ModernFrame 目标“_self”)。这个问题需要几个小时的尝试和搜索,如果您提交一个简单的 2 表单导航器,我将不胜感激。
    • 是否可以发布一个显示问题的代码的工作示例?这将有助于更好地理解问题。
    • 更新了我的答案。我在回答中使用了 Page1.xaml,因为 MainWindow 似乎是从 xaml 中的 ModernWindow 和后面代码中的 UserControl 派生的,我不确定目的,所以我确实使用 Page1 来演示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    相关资源
    最近更新 更多