2014-09-26 30 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