ListView loads by call from builder but not by button

0

I have a listview in my app and I created in my ViewModel a LoadListView () method;

When I call it through the constructor of my ViewModel, an ObservableCollection list is loaded and the binding is given in my view.

When I call the LoadListView () method with a one-button command, the LoadListView () method is called and the list is loaded, but it does not bind in my view.

Does anyone know why?

This is my listView

    <ListView Grid.Row="2"
                      Grid.Column="1"
                      Grid.ColumnSpan="3" 
                      ItemsSource="{Binding ListaIntervalos }" 
                      HasUnevenRows="true">

                <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid> 
                            <Grid.RowDefinitions>
                                <RowDefinition Height="20"/>
                            </Grid.RowDefinitions>

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>

                            <Label Grid.Row="1" Grid.Column="0" Text="{Binding De}"  FontSize="20" VerticalOptions="Start" TextColor="Black"/>
                            <Label Grid.Row="1" Grid.Column="1" Text="{Binding Ate}"  FontSize="20" VerticalOptions="Start" TextColor="Black"/>

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

And in my View Model this way

    public ObservableCollection<Intervalo> ListaIntervalos { get; private set; } = new ObservableCollection<Intervalo>();

    //Construtor
    public VMBuscarSalto()
    {
        // Aqui funciona
        this.CarregaIntervalos();
    }

    public Command BuscaSaltosCommand
    {
        get
        {
            return new Command(() => ExecuteBuscaSaltosCommand());
        }
    }

    private void ExecuteBuscaSaltosCommand()
    { 
        // Aqui a lista é carregada mas não dá o bind no listview
        this.CarregaIntervalos();
    }
    
asked by anonymous 07.10.2017 / 21:47

1 answer

0

I believe that the problem reported was not something I did wrong, but should be bug in the ListView component of Xamarin, which among us, is full of flaws regarding the use of its features using MVVM. p>

I solved my problem using Code Behind, which is simply loading the ItemsSource property of the listview into XAML

    <ListView Grid.Row="2"
                      Grid.Column="1"
                      Grid.ColumnSpan="3"  
                      x:Name="GridDeIntervalos">

and no code behind

     List<Intervalo> listIntervaloModel = new List<Intervalo>();


            for (int i = 0; i < 10; i++)
            {
                listIntervaloModel.Add(new Intervalo
                {
                    De = "teste " + i,
                    Ate = "teste " + i
                });
            }

            GridDeIntervalos.ItemsSource = listIntervaloModel;
    
08.10.2017 / 16:30