Xamarin.Forms PopModalAsync:未观察到任务的异常

我有以下代码:


System.Threading.Tasks.Task appointmentEndTask = App.ArdaBusinessLogic.AppointmentEnd(_appointment);

System.Threading.Tasks.Task appointmentEndCompletedTask = appointmentEndTask.ContinueWith(

    async task =>

    {

        _appointmentDetailPage.IsDirty = true;

        await App.MasterNavigationPage.Navigation.PopModalAsync();

    }, 

    System.Threading.CancellationToken.None, 

    System.Threading.Tasks.TaskContinuationOptions.OnlyOnRanToCompletion, 

    System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext());


System.Threading.Tasks.Task appointmentEndFaultedTask = appointmentEndTask.ContinueWith(

    async task =>

    {

        await App.MasterNavigationPage.Navigation.PopModalAsync();

        await App.ShowErrorPageAsync(task.Exception);

    }, 

    System.Threading.CancellationToken.None, 

    System.Threading.Tasks.TaskContinuationOptions.OnlyOnFaulted, 

    System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext());

因此,如果“AppointmentEnd”任务完成,则应关闭当前模态页面。有时(并非总是!)我在崩溃日志中收到以下错误。在本例中,第 139 行是“_appointmentDetailPage.IsDirty = true”之后的“await App.MasterNavigationPage.Navigation.PopModalAsync()”。

不幸的是,我不明白为什么会出现这个错误。你能帮助我吗?



梵蒂冈之花
浏览 72回答 1
1回答

慕斯709654

首先,为什么要使用如此复杂的语法而不是利用异步等待?public async void EndAppointement(){&nbsp; &nbsp; try&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; await App.ArdaBusinessLogic.AppointmentEnd(_appointment);&nbsp; &nbsp; &nbsp; &nbsp; _appointmentDetailPage.IsDirty = true;&nbsp; &nbsp; &nbsp; &nbsp; await App.MasterNavigationPage.Navigation.PopModalAsync();&nbsp; &nbsp; }&nbsp; &nbsp; catch (Exception exception)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; await App.MasterNavigationPage.Navigation.PopModalAsync();&nbsp; &nbsp; &nbsp; &nbsp; await App.ShowErrorPageAsync(exception);&nbsp; &nbsp; }}二、看XF源码:protected override async Task<Page> OnPopModal(bool animated){&nbsp; &nbsp; Page modal = ModalStack[ModalStack.Count - 1];&nbsp; &nbsp; if (_owner.OnModalPopping(modal))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _owner.OnPopCanceled();&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp; &nbsp; Page result = await base.OnPopModal(animated);&nbsp; &nbsp; result.Parent = null;&nbsp; &nbsp; _owner.OnModalPopped(result);&nbsp; &nbsp; return result;}您的模态堆栈似乎混乱了:这意味着您正在尝试弹出不在堆栈上的页面。您确定您处于模态页面吗?也许使用PopAsync而不是PopModalAsync.
打开App,查看更多内容
随时随地看视频慕课网APP