无法使用c#导航到Windows Metro App上的页面

Sha*_*ack 11 c# navigation microsoft-metro windows-runtime

当我的UserLogin页面加载时,我想检查用户数据库,如果它不存在,或者无法读取,我想将它指向NewUser页面.

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

问题是它永远不会导航NewUser,即使我评论出这个if条件.

Dam*_*Arh 14

Navigate不能直接调用表单OnNavigatedTo方法.你应该调用你的代码Dispatcher,它会工作:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    CheckForUser();
    if (UserExists == false)
        Dispatcher.RunAsync(CoreDispatcherPriority.Normal, 
                            () => this.Frame.Navigate(typeof(NewUser)));
}
Run Code Online (Sandbox Code Playgroud)