【发布时间】:2017-12-08 00:35:21
【问题描述】:
我正在使用 Xamarin Forms,但在将 InsertPageBefore() 方法与 Pages 的现有对象结合使用时遇到问题。
这是我的查看代码:
private FirstPage firstPage;
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;
}
它抛出异常“System.ArgumentException:'无法插入已经在导航堆栈中的页面'”。如何在现有的页面对象之间切换?我不想在 InsertPageBefore() 中创建新对象。在调用 InsertPageBefore() 之前,我尝试使用它的代码:
foreach (var item in App.NavigationPage.Navigation.NavigationStack.ToList())
App.NavigationPage.Navigation.RemovePage(item);
但它不起作用......谁能帮助我?
【问题讨论】:
-
stackoverflow.com/questions/44865472/… 的可能重复项您要达到什么目的?为什么你使用 Insert 和 Pop 而不是 Push?
-
我想要它的菜单:csharp-dev.pl/wp-content/uploads/2017/04/5.png 当我点击项目时,他的页面显示为“根页面”。
-
看起来您正在使用主/详细页面。你的 NavigationPage 详细吗?
-
是的。 rootPage.Detail = 导航页;它的代码适用于 Android 的旧版 Xamarin,但不适用于新版 Xamain 和 Windows 10 Mobile。
-
当我使用这一行时:App.NavigationPage.Navigation.InsertPageBefore(new ThirdPage(), root);一切正常,但我不想在 args 中为此方法(InsertPageBefore)创建新对象。
标签: c# xamarin xamarin.forms