xamarin / XamarinCommunityToolkit

The Xamarin Community Toolkit is a collection of Animations, Behaviors, Converters, and Effects for mobile development with Xamarin.Forms. It simplifies and demonstrates common developer tasks building iOS, Android, and UWP apps with Xamarin.Forms.
MIT License
1.59k stars 471 forks source link

[Bug] StateView System.InvalidOperationException: Collection was modified; enumeration operation may not execute. #880

Open ValonK opened 3 years ago

ValonK commented 3 years ago

Description

Im getting this Crash when using StateLayout like this

<Grid toolkit:StateLayout.CurrentState="{Binding CurrentState}"
      toolkit:StateLayout.AnimateStateChanges="True"
      toolkit:StateLayout.CurrentCustomStateKey="{Binding CustomStateKey}">
     <toolkit:StateLayout.StateViews>
        <toolkit:StateView StateKey="Loading">
            <states:LoadingStateView/>
        </toolkit:StateView>
        <toolkit:StateView StateKey="Custom" CustomStateKey="{x:Static helpers:StateKeys.NotAuthenticated}">
            <states:NotAuthenticatedStateView />
        </toolkit:StateView>
        <toolkit:StateView StateKey="Success">
            <ContentView Content="{StaticResource SuccessView}"/>
        </toolkit:StateView>
        <toolkit:StateView StateKey="Empty">
            <states:EmptyView  />
        </toolkit:StateView>
    </toolkit:StateLayout.StateViews>
</Grid>
internal async void OnRefreshAsync()
{
    if (!IsAuthenticated)
    {
        return;
    }

    CurrentState = LayoutState.Loading;
    try
    {
        await LoadItems();
    }
    finally
    }
        CurrentState = LayoutState.Success;
    }
}
android.runtime.JavaProxyThrowable: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Collections.Generic.List`1+Enumerator[T].MoveNextRare () [0x00013] in <9e65554b56364c219ee375864f4fd8c4>:0 
at System.Collections.Generic.List`1+Enumerator[T].MoveNext () [0x0004a] in <9e65554b56364c219ee375864f4fd8c4>:0 
at System.Linq.Enumerable+WhereEnumerableIterator`1[TSource].MoveNext () [0x0004e] in <b943f5a0a8474cebaa297e667e2095bb>:0 
at System.Linq.Enumerable+<OfTypeIterator>d__32`1[TResult].MoveNext () [0x00067] in <b943f5a0a8474cebaa297e667e2095bb>:0 
at System.Linq.Enumerable+SelectIListIterator`2[TSource,TResult].MoveNext () [0x00029] in <b943f5a0a8474cebaa297e667e2095bb>:0 
 at System.Threading.Tasks.Task.WhenAll[TResult] (System.Collections.Generic.IEnumerable`1[T] tasks) [0x000ba] in <9e65554b56364c219ee375864f4fd8c4>:0 
 at Xamarin.CommunityToolkit.UI.Views.StateLayoutController.ChildrenFadeTo (Xamarin.Forms.Layout`1[T] layout, System.Boolean animate, System.Boolean isHide) [0x000ae] in <d695d744715f441683440b16d4c7616b>:0 
 at Xamarin.CommunityToolkit.UI.Views.StateLayoutController.SwitchToTemplate (Xamarin.CommunityToolkit.UI.Views.LayoutState state, System.String customState, System.Boolean animate) [0x003b8] in <d695d744715f441683440b16d4c7616b>:0 
 at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in <9e65554b56364c219ee375864f4fd8c4>:0 
  at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in <ab7a92db3c6e45c6ae5846565187cb8b>:0 
at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <ab7a92db3c6e45c6ae5846565187cb8b>:0 
at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in <ab7a92db3c6e45c6ae5846565187cb8b>:0 
 at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.27(intptr,intptr)
AndreiMisiukevich commented 3 years ago

@ValonK https://github.com/xamarin/XamarinCommunityToolkit/issues/784

https://github.com/xamarin/XamarinCommunityToolkit/issues/784#issuecomment-768207140

jfversluis commented 3 years ago

So, this is not a bug? Or what is going on with this one?

AndreiMisiukevich commented 3 years ago

@ValonK can you reproduce this issue on the latest nuget version? If yes, then please attach a sample project. Thanks!