11

When my UserLogin page loads, i want to check for user database, and if it doesn't exist, or can't be read, i want to direct it to NewUser page.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    CheckForUser();
    if (UserExists == false)
        this.Frame.Navigate(typeof(NewUser));
}

The problem is that it never navigates to NewUser, even when i comment out the if condition.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Shashwat Black
  • 992
  • 5
  • 13
  • 24
  • 2
    have you check MSDN ** here is a link http://msdn.microsoft.com/en-us/library/windows/apps/br211386.aspx ** – MethodMan Dec 11 '12 at 17:13
  • I did not think you could navigate in the `OnNavigatedTo` method because you are still in the middle of a navigation. – Trisped Dec 12 '12 at 02:36

6 Answers6

14

Navigate can't be called directly form OnNavigatedTo method. You should invoke your code through Dispatcher and it will work:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    CheckForUser();
    if (UserExists == false)
        Dispatcher.RunAsync(CoreDispatcherPriority.Normal, 
                            () => this.Frame.Navigate(typeof(NewUser)));
}
Damir Arh
  • 17,637
  • 2
  • 45
  • 83
  • Be sure to call base.OnNavigatedTo(e) at the top of your overrided method call. I had this same code, minus the base.OnNavigatedTo(e) and I was still getting an exception. This answer helped me out, thank you! – Allen Rufolo Nov 15 '16 at 18:38
1

This happens because your app tries to navigate before the current frame completely loaded. Dispatcher could be a nice solution, but you have to follow the syntax bellow.

using Windows.UI.Core;

    private async void to_navigate()
    {
        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.Frame.Navigate(typeof(MainPage)));
    }
  1. Replace MainPage with your desired page name.
  2. Call this to_navigate() function.
SamSmart
  • 355
  • 3
  • 3
0

you can try this and see if this works

frame.Navigate(typeof(myPage)); // the name of your page replace with myPage

full example

    var cntnt = Window.Current.Content;
    var frame = cntnt as Frame;

    if (frame != null)
    { 
        frame.Navigate(typeof(myPage));
    }
    Window.Current.Activate();

or

if you want to use a 3rd party tool like Telerik try this link as well

Classic Windows Forms, Stunning User Interface

MethodMan
  • 18,625
  • 6
  • 34
  • 52
0

I see you override OnNavigatedTo method but do not call base method. It may be the source of problem. Try calling base method before any logic:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    CheckForUser();
    if (UserExists == false)
        this.Frame.Navigate(typeof(NewUser));
}
imslavko
  • 6,596
  • 2
  • 34
  • 43
0

Use Dispatcher.RunIdleAsync to postpone your navigation to another page until UserLogin page is completely loaded.

maxim pg
  • 701
  • 4
  • 9
0

The others are correct, but since Dispatcher doesn't work from the view model, here's how to do it there:

SynchronizationContext.Current.Post((o) =>
{
    // navigate here
}, null);
Chris Bordeman
  • 255
  • 7
  • 19