2014-09-26 27 views
0

我在尝试在基于一点检查的Windows 8.1应用程序中的页面之间自动导航时遇到问题。它只是不想在LoadState中这样做时导航到另一个页面,就好像某些东西尚未加载,但它也不会给出错误。当我在做Frame.Navigate之前使用(例如)await Task.Delay(2000)插入延迟时,那么我的应用程序将无任何问题地重定向。Frame.Navigate in LoadState

protected async override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState) 
    { 
     MyData oData = await getData(); 

     if (oData != null) 
     { 
      this.Frame.Navigate(typeof(newPage), oData); 
     } 
     else 
     { 
      // do something else 
     } 

    } 

是否必须将此代码放入另一个加载事件或导航事件?或者我该如何做这项工作?

回答

0

LoadStateSaveState中,您应该只保存并恢复页面状态(在暂停和重新激活应用程序时调用)。别做任何事情(如导航)。

把你的逻辑到OnNavigatedTo方法,而不是...

0

如果你想从方法调用时,页面加载是导航,你应该把你的导航代码的OnNavigatedTo(...)。但是不要忘了来包装你的代码Dispatcher.RunAsync(...) - Frame navigation in xaml return false

0

我试图从的OnNavigatedTo方法调用Frame.Navigate(...),但仍没有发生导航。

还有其他答案说使用Dispatcher.RunAsync,但感觉就像是在对Windows Phone的线程模型做出假设。

以下是我的工作:将一个处理程序附加到页面的加载的事件中,并将“重定向”逻辑放在那里。在OnNavigateTo之后和NavigationHelper_LoadState之后,但在页面变得可见之前加载了火焰。

public LaunchPadPage() { 
     this.InitializeComponent(); 

     this.navigationHelper = new NavigationHelper(this); 
     this.navigationHelper.LoadState += this.NavigationHelper_LoadState; 
     this.navigationHelper.SaveState += this.NavigationHelper_SaveState; 

     this.Loaded += LaunchPadPage_Loaded; 

     this.app = (App)App.Current; 
    } 

    private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e) { 
     // Let's show the root zone items 
     // NB: In case we don't have this data yet, do nothing 
     if (app.Hierarchy != null) 
     { 
      DefaultViewModel["Items"] = app.Hierarchy.RootItems; 
     } 
    } 

    private void LaunchPadPage_Loaded(object sender, RoutedEventArgs e) { 
     // No data? Go to the downloads page instead. 
     if (app.Hierarchy == null) 
     { 
      Frame.Navigate(typeof(DownloadingPage)); 
     } 
    } 
+0

什么是'NavigationHelper'? – Denny 2016-09-12 10:53:09