简体   繁体   中英

How to remove the execution of a method from a ViewModel - Xamarin.Forms - MVVM

I have a <List> that adds and removes elements by calling a modal from a <Button> as shown in the figure.

列表

When lifting the modal, I show the user the elements that can be associated to the main list and also indicate with a <Switch> if these elements are added or not, with the objective that when pressing the switch add or remove elements from the main list

模态

The problem is that when the modal is lifted, the method that adds and deletes elements to the list is executed and every time I raise the modal, my records are duplicated, as shown in the figure

重复

This is due to the fact that when lifting the modal, the method that adds or removes chemicals is executed, this is called every time the property value that is binded to the <Switch> in the view changes

Why is this happening? How can I avoid it?

Then I present my code...

MODAL.XAML:

<StackLayout           
            BindingContext="{Binding AgregarSustancia}">

      <ListView
                    ItemsSource="{Binding ListaSustancias}"
                    SelectedItem="{Binding SelectedSustancia}">                      
                     <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout>

                                    <StackLayout Orientation="Vertical">

                                          <Label Text="{Binding NombreSustancia}"   
                                                   HorizontalOptions="FillAndExpand"   
                                                  VerticalOptions="CenterAndExpand"/>

                                    </StackLayout>

                                            <Switch
                                               OnColor="{StaticResource das.color.verde}"              HorizontalOptions="EndAndExpand"
                                               VerticalOptions="Start"
                                               IsToggled="{Binding SustanciaAsociada, Mode=OneWay}">
                                           </Switch>

                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

<StackLayout/>

AGREGARSUSTANCIA.CS:

     bool sustanciaAsociada;

     [JsonProperty(PropertyName = "chemicalIsAssociateWithInstallation")]
            public bool SustanciaAsociada
            {

                get
                {
                    return sustanciaAsociada;
                }

                set
                {
                    if (value != sustanciaAsociada)
                    {
                        sustanciaAsociada = value;

                        AsociarDesasociar(sustanciaAsociada);
                    }
                }
            }

//METHOD THAT ADDS OR ELIMINATES DEPENDING ON THE VALUE OF THE PARAMETER
  private async void AsociarDesasociar(bool sustanciaAsociada)
        {                      

            //ADD TO LIST
            if (sustanciaAsociada)
            {

            }

            else //REMOVE TO LIST
            {


            }
        }

Then my ViewModel that fills the modal list

VIEWMODEL.CS: (MODAL)

  #region Constructor
        public AgregarSustanciaViewModel(ObservableCollection<AgregarSustancia> listaAgregarSustancia)
        {
            navigationService = new NavigationService();

            ListaSustancias = new ObservableCollection<AgregarSustancia>();

            listaSustanciasAux = new List<AgregarSustancia>(listaAgregarSustancia);

            ListaSustancias = listaAgregarSustancia;

        }
        #endregion

How can I prevent the method AsociarDesasociar() in the Get-Set property of the Switch from executing the modal? How can I encapsulate this method? Any help for me?

create a bool InitComplete and initialize it to false . This will prevent AsociarDesasociar from executing before initialization is complete

if (value != sustanciaAsociada)
{
  sustanciaAsociada = value;

  if (InitComplete) {
    AsociarDesasociar(sustanciaAsociada);
  }
}

after your class has finished whatever initialization is required, then set InitComplete = true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM