Modern ui wpf navigation

I use modern ui wpf and try to switch from the CheckLogin.xaml page to the MainWindow.xaml page (they are in the root directory of the solution). From inside CheckLogin.xaml I wrote the following:

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

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

but the exception is thrown: "Could not go to package: // application: /MainWindow.xaml, could not find the target of ModernFrame."

What am I missing and how to move?

+4
source share
1 answer

Using NavigationService

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

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

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

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

Source: https://habr.com/ru/post/1544581/


All Articles