之前必须在当前上下文的推送堆栈中



我正在使用Xamarin Form PCL创建一个iOS应用程序。页面的流程为Login page->Page Main->Page OrderList->Page OrderItem->Page Signature->Page Camera.问题是从主页到相机页面我可以没有任何问题,但是当我尝试从相机页面返回到订单列表页面时,我收到异常before must be in the pushed stack of the current context。我不知道出了什么问题。因为相同的代码在Android中完美运行,但在iOS中无法运行。

Navigation.InsertPageBefore(new OrderList(),  this);
await Navigation.PopAsync();

完全异常

Message: before must be in the pushed stack of the current context
InnerMessage: 
StackTrace:   at Xamarin.Forms.Internals.NavigationProxy.OnInsertPageBefore (Xamarin.Forms.Page page, Xamarin.Forms.Page before) [0x00020] in D:agent_work1sXamarin.Forms.CoreNavigationProxy.cs:150 
at Xamarin.Forms.Internals.NavigationProxy.InsertPageBefore (Xamarin.Forms.Page page, Xamarin.Forms.Page before) [0x00000] in D:agent_work1sXamarin.Forms.CoreNavigationProxy.cs:59 
at AT.PictureAfterSignature+<Process>d__9.MoveNext () [0x000a2] in E:ProjectsxamrineSourceATiOSATATPictureAfterSignature.xaml.cs:422 
Source: Xamarin.Forms.Core

希望有人知道如何解决这个问题。

如果收到此错误,可能是因为死页正在调用该函数。例如,如果您通过以下方式在主页中使用订阅事件处理页面过渡

MessagingCenter.Subscribe<Application>();

当您通过 PopAsync() "离开"主页(例如,转到登录页面,登录后将创建一个新的主页)时,您需要首先取消订阅任何事件,否则下次调用导航事件时,死页面仍在侦听,并在已经删除时再次尝试删除自己。我希望这是有道理的。

您还可以检查Navigation.NavigationStack是否为空,如果是,则返回,但这并不能解决根本问题。

最新更新