Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to navigate to pages on Windows Metro App using c#

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.

like image 967
Shashwat Black Avatar asked Dec 11 '12 17:12

Shashwat Black


2 Answers

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)));
}
like image 94
Damir Arh Avatar answered Nov 15 '22 08:11

Damir Arh


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.
like image 36
SamSmart Avatar answered Nov 15 '22 08:11

SamSmart