4

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?

user3222589
  • 184
  • 1
  • 13

1 Answers1

10

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";
pushpraj
  • 13,458
  • 3
  • 33
  • 50
  • bbBlock.LinkNavigator.Navigate doesn't have an overload that takes one argument! Have you tried your solution before submition? – user3222589 Jun 15 '14 at 12:13
  • thanks for posting back @pushpraj. I tried all options and got this exception (Unable to navigate to /MainWindow.xaml, could not find a ModernFrame target '_self'). This issue took hours of tries and searches, if you kindly submitted a simple 2 forms navigator I would appreciate it. – user3222589 Jun 15 '14 at 15:56
  • is it possible to post a working sample of your code, which shows the issue? that would help understand the issue better. – pushpraj Jun 15 '14 at 16:31
  • Sample is in the link: https://www.dropbox.com/s/18wapc6nfj93vot/NavigationTest.zip – user3222589 Jun 15 '14 at 18:55
  • updated my answer. I used Page1.xaml in answer as MainWindow seems to be derived from ModernWindow in xaml and UserControl in code behind, I was not sure about the purpose so I did use Page1 to demonstrate the same. – pushpraj Jun 16 '14 at 06:03
  • The navigation to Page1.xaml succeeded using methods you prvided above. I derived MainMenu.xaml from ModernWindow to be able to add navigation , and can't find a way to add menulink to a window derived from UserControl because UserControl window can be nested inside a Modern Window window. To overcome the problem, we have two methods: 1. To add navigation to Page1.xaml (may be impossible) or 2. find a way to navigate to MainWindow (and this should be easy, I can't find a reasonable reason why this appears to need a miracle to be done! this is a basic need) – user3222589 Jun 16 '14 at 13:47