0

I have a list view in Xamarin Froms Project as :

 <ListView x:Name="ExerciseList" HasUnevenRows="False" SeparatorVisibility="None" RowHeight="200">

                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Orientation="Vertical">
                                <StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
                                    <Entry Text="{Binding ExerciseName}" HorizontalTextAlignment="Center" Focused="ExerciseName_Focused" HorizontalOptions="CenterAndExpand">
                                        <Entry.GestureRecognizers>
                                            <TapGestureRecognizer Tapped="ExerciseNameGestureRecognizer_Tapped"/>
                                        </Entry.GestureRecognizers>
                                    </Entry>
                                    <Image IsVisible="{Binding GreenVisible}" Source="smallgreenadd.png"/>
                                    <Image IsVisible="{Binding RedVisible}" Source="smallredremove.png"/>
                                </StackLayout>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="1*"/>
                                        <ColumnDefinition Width="1*"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="1*"/>
                                        <RowDefinition Height="1*"/>
                                        <RowDefinition Height="1*"/>
                                    </Grid.RowDefinitions>
                                    <Label Text="Sets : " Grid.Column="0" Grid.Row="0" HorizontalTextAlignment="Center" HorizontalOptions="CenterAndExpand" />
                                    <Label Text="Weights : " Grid.Column="0" Grid.Row="1" HorizontalTextAlignment="Center" HorizontalOptions="CenterAndExpand" />
                                    <Label Text="Reps: " Grid.Column="0" Grid.Row="2" HorizontalTextAlignment="Center" HorizontalOptions="CenterAndExpand" />
                                    <Entry Text="{Binding Sets}" Grid.Column="1" Grid.Row="0" HorizontalTextAlignment="Center" HorizontalOptions="CenterAndExpand" />
                                    <Entry Text="{Binding Weights}" Grid.Column="1" Grid.Row="1"  HorizontalTextAlignment="Center" HorizontalOptions="CenterAndExpand" />
                                    <Entry Text="{Binding Reps}" Grid.Column="1" Grid.Row="2"  HorizontalTextAlignment="Center" HorizontalOptions="CenterAndExpand" />
                                </Grid>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>               
            </ListView>

This is attached to a View Modal called ExerciseViewModal. This is:

public class ExerciseViewModal : BaseViewModal
{
    private List<AddExerciseModal> _addExerciseModals;
    public List<AddExerciseModal> AddExerciseModals
    {
        get { return _addExerciseModals; }
        set
        {
            _addExerciseModals = value;
            OnPropertyChanged("AddExerciseModals");
        }
    }
    public ExerciseViewModal()
    {
        _addExerciseModals = new List<AddExerciseModal>();

        if (AddExerciseModals.Count == 0)
        {
        for (int i = 0; i < 7; i++)
        {
                AddExerciseModal addExerciseModal = new AddExerciseModal
                {
                    ExerciseID = i,
                    ExerciseName = "Excercise " + i,
                    GreenVisible = false,
                    RedVisible = true,
                    Sets = "2",
                    Reps = "10",
                    Weights = "10"
                };
                AddExerciseModals.Add(addExerciseModal);
        }
            AddExerciseModals[AddExerciseModals.Count - 1].GreenVisible = true;
            AddExerciseModals[AddExerciseModals.Count - 1].RedVisible = false;
        }

    }
}

AddExerciseModal class :

public class AddExerciseModal
{
  public int ExerciseID { get; set; }
  public string ExerciseName { get; set; } 
  public string Weights { get; set; }
  public string Reps { get; set; }
  public string Sets { get; set; }
  public bool GreenVisible { get; set; }
  public bool RedVisible { get; set; }
}

Whenever I try to change the sets/reps/Weights property inside the ListView I always get an error saying:

"Collection was modified; enumeration operation may not execute."

How can I solve this?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Subash
  • 137
  • 1
  • 2
  • 15
  • Could you please also include sources for `AddExerciseModal`? – bashis May 01 '18 at 02:07
  • 1
    Possible duplicate of [Collection was modified; enumeration operation may not execute](https://stackoverflow.com/questions/604831/collection-was-modified-enumeration-operation-may-not-execute) or https://stackoverflow.com/q/2024179 or [26,000 more](https://www.google.com/search?rlz=1C1CHFX_enUS460US460&ei=otDnWtyPDcfEjwTmm5LoBw&q=Collection+was+modified%3B+enumeration+operation+may+not+execute+c%23+site%3Astackoverflow.com&oq=Collection+was+modified%3B+enumeration+operation+may+not+execute+c%23+site%3Astackoverflow.com) – Ňɏssa Pøngjǣrdenlarp May 01 '18 at 02:28
  • @bashis I have added the class as suggested. – Subash May 01 '18 at 17:08
  • Use observableCollection instead of List in your ViewModel. AND don't forget to set "TwoWay" mode in your bindings, if you want to update values from UI. FINALLY take care of "AddExerciseModel" class that doesn't implement "INotifyPropertyChanged" interface (or doesn't inherit from BaseViewModel to call 'OnProperyChanged method in setters). Actually values will not be updated... – KFactory May 02 '18 at 15:38
  • @Julien Thanks for reminding me about "TwoWay" binding. I did all you stated but still the error is same. – Subash May 02 '18 at 17:40

1 Answers1

0

Can you change your List to ObservableCollection and try it .That will resolve the problem.

Adit Kothari
  • 421
  • 3
  • 16