OPunktSchmidt
OPunktSchmidt

Reputation: 721

Xamarin.Forms PopModalAsync: A Task's exception(s) were not observed

I have the following Code:

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());

So if the "AppointmentEnd"-Task completes the current Modal-Page should be closed. From time to time (not always!) i get the following error in my crash logs. Linenumber 139 is "await App.MasterNavigationPage.Navigation.PopModalAsync()" after "_appointmentDetailPage.IsDirty = true" in this case.

Xamarin caused by: android.runtime.JavaProxyThrowable:
System.AggregateException: A Task's exception(s) were not observed
either by Waiting on the Task or accessing its Exception property. As
a result, the unobserved exception was rethrown by the finalizer
thread. ---System.ArgumentOutOfRangeException: Index was out of
range. Must be non-negative and less than the size of the collection.
Parameter name: index   at
System.Collections.Generic.List`1[T].get_Item (System.Int32 index)
[0x00009] in
/Users/builder/jenkins/workspace/xamarin-android-d15-9/xamarin-android/external/mono/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/List.cs:180
at Xamarin.Forms.Application+NavigationImpl+<OnPopModal>d__2.MoveNext
() [0x00022] in D:\a\1\s\Xamarin.Forms.Core\Application.cs:381
--- End of stack trace from previous location where exception was thrown ---   at
CPM.Arda.Mobile.Freelancer.Ui.Pages.Appointment.Complete+<<confirmButton_Clicked>b__5_0>d.MoveNext
() [0x0007d] in
D:\ProjekteTFVC\ArdaMobileFreelancer\SourceCode\Ui\1.0\Pages\Appointment\Complete.xaml.cs:139
--- End of inner exception stack trace ---
---(Inner Exception #0) System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the
collection. Parameter name: index   at
System.Collections.Generic.List`1[T].get_Item (System.Int32 index)
[0x00009] in
/Users/builder/jenkins/workspace/xamarin-android-d15-9/xamarin-android/external/mono/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/List.cs:180
at Xamarin.Forms.Application+NavigationImpl+<OnPopModal>d__2.MoveNext
() [0x00022] in D:\a\1\s\Xamarin.Forms.Core\Application.cs:381
--- End of stack trace from previous location where exception was thrown ---   at
CPM.Arda.Mobile.Freelancer.Ui.Pages.Appointment.Complete+<<confirmButton_Clicked>b__5_0>d.MoveNext
() [0x0007d] in
D:\ProjekteTFVC\ArdaMobileFreelancer\SourceCode\Ui\1.0\Pages\Appointment\Complete.xaml.cs:139

Unfortunately, I do not understand how it can come to this error. Can you help me?

Upvotes: 0

Views: 811

Answers (1)

Roubachof
Roubachof

Reputation: 3401

First, why are you using such a complicated syntax instead of taking advantage of async await?

public async void EndAppointement()
{
    try 
    {
        await App.ArdaBusinessLogic.AppointmentEnd(_appointment);
        _appointmentDetailPage.IsDirty = true;
        await App.MasterNavigationPage.Navigation.PopModalAsync();
    }
    catch (Exception exception)
    {
        await App.MasterNavigationPage.Navigation.PopModalAsync();
        await App.ShowErrorPageAsync(exception);
    }
}

Second, looking at XF source code:

protected override async Task<Page> OnPopModal(bool animated)
{
    Page modal = ModalStack[ModalStack.Count - 1];
    if (_owner.OnModalPopping(modal))
    {
        _owner.OnPopCanceled();
        return null;
    }
    Page result = await base.OnPopModal(animated);
    result.Parent = null;
    _owner.OnModalPopped(result);
    return result;
}

It seems that your modal stack is messed up: meaning you are trying to pop pages that are not on the stack. Are you sure you're on a modal page? Maybe use PopAsync instead of PopModalAsync.

Upvotes: 2

Related Questions