modern ui wpf navigation

梦想与她 提交于 2019-12-05 02:21:58

问题


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

I used the following values for 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 ''".

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


回答1:


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

    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.

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


来源:https://stackoverflow.com/questions/24221571/modern-ui-wpf-navigation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!