2017-07-01 72 views
0

我使用Xamarin Forms(2.3.4.247),我的應用程序使用「HamburgerMenu」。要我使用它的頁面之間切換代碼:在頁面之間切換導致Windows 10移動版崩潰

private FirstPage firstPage; //it's i get from the constructor 
private SecondPage secondPage = new SecondPage(); 
private ThirdPage thirdPage = new ThirdPage(); 
private async void ItemSelectedMethod() 
{ 
     var root = App.NavigationPage.Navigation.NavigationStack[0]; 
     if (SelectedItem == Items[0]) 
     { 
      if (!IsFirstChoose) 
      { 
       App.NavigationPage.Navigation.InsertPageBefore(firstPage, root); 
       await App.NavigationPage.PopToRootAsync(false); 
      } 
     } 
     if (SelectedItem == Items[1]) 
     { 
      App.NavigationPage.Navigation.InsertPageBefore(secondPage, root); 
      await App.NavigationPage.PopToRootAsync(false); 
     } 
     if (SelectedItem == Items[2]) 
     { 
      App.NavigationPage.Navigation.InsertPageBefore(thirdPage, root); 
      await App.NavigationPage.PopToRootAsync(false); 
     } 

     IsFirstChoose = false; 
     rootPageViewModel.IsPresented = false; 
} 

所有工作好Android和Windows 10桌面上,就當我thirdPage和第一頁之間切換的Windows 10手機模擬器我的應用程序崩潰。 第一頁是根:

FirstPage firstPage = new FirstPage(); 
NavigationPage = new NavigationPage(firstPage); 

我不知道爲什麼......模擬器不允許調試......

的第二件事: 當我更新Xamarin形式來2.3.5.256-版本PRE6我的應用程序拋出異常「System.ArgumentException:‘無法插入這已經是在導航棧頁面’」 ......但是,當我更改代碼:

App.NavigationPage.Navigation.InsertPageBefore(new ThirdPage(), root); 
App.NavigationPage.Navigation.InsertPageBefore(new SecondPage(), root); 
//etc 

所有工作... 有誰知道爲什麼這是怎麼回事?我不希望創建新的對象時,頁面是開關...

回答

0

你已經回答了這個問題,所以我只能證實了這一點:

System.ArgumentException: 'Cannot insert page which is already in the navigation stack' 

正如你所看到的,你已經在頁面第一頁App.NavigationPage.Navigation因此插入另一個會導致應用程序崩潰。你不能像你說的那樣去做 - 要麼你必須創建一個新的實例,或者你必須從堆棧中刪除以前的實例。

+0

是的,但爲什麼它沒有扔在版本2.3.4.247?爲什麼應用程序只在Win10Mobile上崩潰? – luki

+0

Xamarin正在將您的呼叫轉變爲本地課程。每個操作系統都有支持導航的本機類,它們可能會有所不同。它顯然不適用於Windows 10 Mobile。爲什麼它不會出現在以前版本的Xamarin中?可能是因爲某些東西沒有起作用,因爲它應該是Xamarin更新的原因之一。 –

+0

你可能是對的。所以...在Android和Windows Mobile上工作的代碼應該是什麼? (現在我的代碼只能在Android和Windows桌面上工作)。 – luki