在页面之间切换会导致Windows 10移动版崩溃 [英] Switching between pages causes crashes on Windows 10 Mobile
问题描述
我正在使用Xamarin Forms(2.3.4.247),而我的应用程序正在使用"HamburgerMenu".要在页面之间切换,我使用的是代码:
I'm using Xamarin Forms (2.3.4.247) and my application is using "HamburgerMenu". To switch between pages i'm using it's code:
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桌面上,以及在Windows 10 Mobile模拟器上,一切正常,当我在thirdPage和firstPage之间切换时,我的应用程序崩溃了. FirstPage是根目录:
All working good on Android and Windows 10 desktop, on Windows 10 Mobile simulator my app crashes when I switch between thirdPage and firstPage. FirstPage is root:
FirstPage firstPage = new FirstPage();
NavigationPage = new NavigationPage(firstPage);
我不知道为什么...模拟器不允许调试...
I don't know why... Simulator don't allow debugging...
第二件事: 当我将Xamarin Forms更新到版本2.3.5.256-pre6时,我的应用程序抛出异常"System.ArgumentException:'无法插入导航堆栈中已经存在的页面'" ...但是当我将代码更改为:
The second thing: When I update Xamarin Forms to version 2.3.5.256-pre6 my app throw exception "System.ArgumentException: 'Cannot insert page which is already in the navigation stack'"... But when I change code to:
App.NavigationPage.Navigation.InsertPageBefore(new ThirdPage(), root);
App.NavigationPage.Navigation.InsertPageBefore(new SecondPage(), root);
//etc
所有工作中... 有人知道为什么会这样吗?我不想在切换页面时创建新对象...
all working... Does anyone know why this is happening? I don't want create new objects when pages is switch...
推荐答案
您已经回答了这个问题,所以我只能确认一下:
You have answered the question so I can only confirm this:
System.ArgumentException: 'Cannot insert page which is already in the navigation stack'
如您所见,您已经在App.NavigationPage.Navigation
中拥有firstPage页面,因此插入另一个页面会使应用程序崩溃.您只是无法按照说明进行操作-您必须创建一个新实例,或者必须从堆栈中删除以前的实例.
As you can see, you already have the page firstPage in App.NavigationPage.Navigation
so inserting another one makes the app crash. You just can't do it as you have explained - either you have to create a new instance or you have to remove previous instance from the stack.
这篇关于在页面之间切换会导致Windows 10移动版崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!