MAUI: ListView not updating in iOS (works on Android)

Ayleen 21 Reputation points
2022-12-09T16:18:43.313+00:00

I have a ListView, which is populated by an add function like so:

268957-list-empty.png
(Fig 1: empty list)

268958-list-one.png
(Fig 2: one item added)

Data binding with MVVM:

<ListView ItemsSource="{Binding Persons}">  
      <Label Text="{Binding .}"  

The Entry field is bound to:

[ObservableProperty]  
public String attendee;  

Persons is an ObservableCollection in the viewmodel:

public ViewModel()  
        {  
            Persons = new ObservableCollection<String>();  
        }  
  
        [ObservableProperty]  
        ObservableCollection<String> persons;  

Button code:

[RelayCommand]  
void AddPerson()  
{  
    Persons.Add(Attendee);  
    Attendee = string.Empty;  
}  

Works on Android as shown above. Does not work on iOS, although the added persons are in the collection as they should. The list does just not get updated:

268937-list-ios.png
(Fig 3: list newer appears on iOS)

Developer technologies | .NET | .NET MAUI
{count} votes

2 answers

Sort by: Most helpful
  1. Ayleen 21 Reputation points
    2022-12-13T18:20:13.857+00:00

    I tried to post that as a reply to @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) , but it didn't work ¯_(ツ)_/¯

    Thank you for your effort - Sorry for sharing possibly too less code.

    You are right, Entry and Button are not inside the ListView:

    < ... more stuff above ...>  
      
        <Label Text="Attendees"  
               Padding="0, 10"/>  
        <Border>  
            <VerticalStackLayout>  
                <ListView  
                    ItemsSource="{Binding Persons}">  
                    <ListView.ItemTemplate>  
                        <DataTemplate>  
                            <ViewCell>  
                                <Grid RowDefinitions="*, Auto">  
                                    <Label Text="{Binding .}">  
                                    <BoxView HeightRequest="1"/>  
                                </Grid>  
                             </ViewCell>  
                        </DataTemplate>  
                    </ListView.ItemTemplate>  
                </ListView>  
                <Grid ColumnDefinitions="*, 50">  
                    <Entry Placeholder="New attendee"  
                                Text="{Binding Attendee}"/>  
                    <Button ImageSource="circle_plus_active.png"  
                            BackgroundColor="Transparent"  
                            Command="{Binding AddPersonCommand}"  
                            Grid.Column="1"/>  
                </Grid>  
             </VerticalStackLayout>  
        </Border>  
    

    I tried your XAML, and if I use it isolated (=> takes full height), it works. As soon as I wrap it in a Border element or have other elements above, it does not. So this seems to be the restricting element in the iOS implementation.

    As a workaround, I now use this:

               <Grid>  
                    <Grid.RowDefinitions>  
                        <RowDefinition Height="*" />  
                        <RowDefinition Height="Auto" />  
                    </Grid.RowDefinitions>  
                    <ListView  
                        ItemsSource="{Binding Persons}">  
                        <ListView.ItemTemplate>  
                            <DataTemplate>  
                                <ViewCell>  
                                    ...  
                                 </ViewCell>  
                            </DataTemplate>  
                        </ListView.ItemTemplate>  
                    </ListView>  
                    <Grid ColumnDefinitions="*, 50"  
                          Grid.Row="1">  
                        <Entry Text="{Binding Attendee}"/>  
                        <Button ImageSource="circle_plus_active.png"  
                            BackgroundColor="Transparent"  
                            Command="{Binding AddPersonCommand}"  
                            Grid.Column="1"/>  
                    </Grid>  
                 </Grid>  
    

    This makes the Border element take the remaining space on the screen, so there is some space for the ListView to build up.


  2. Ayleen 21 Reputation points
    2022-12-30T10:15:42.693+00:00

    The problem somehow solved itself, when I added a IsVisible property to my button. It seems, that this change in visibility does somehow trigger something to update the whole content that's wrapped inside the VerticalStackLayout.

    Code:

                        <VerticalStackLayout>  
                            <ListView  
                                ItemsSource="{Binding Persons}"  
                                SelectionMode="None"  
                                SeparatorVisibility="None">  
                                <ListView.ItemTemplate>  
                                    <DataTemplate>  
                                        <ViewCell>  
                                            <Grid RowDefinitions="*, Auto"  
                                                  ColumnSpacing="5"  
                                                  RowSpacing="8"  
                                                  Padding="5">  
                                                <Label Text="{Binding .}"  
                                                       Style="{StaticResource MediumLabel}"  
                                                       TextColor="Black"  
                                                       Padding="0"/>  
                                                <BoxView HeightRequest="1"  
                                                         Margin="0"  
                                                         Grid.Row="1"  
                                                         Color="LightGray"/>  
                                            </Grid>  
                                         </ViewCell>  
                                    </DataTemplate>  
                                </ListView.ItemTemplate>  
                            </ListView>  
                            <Grid ColumnDefinitions="*, Auto"  
                                  Grid.Row="1">  
                                <Entry Placeholder="New attendee"  
                                       PlaceholderColor="DarkGray"  
                                       Style="{StaticResource SmallLabel}"  
                                       TextColor="Black"  
                                       Text="{Binding Attendee}"/>  
                                <Button ImageSource="circle_plus_active.png"  
                                        IsVisible="{Binding btnVisible}"  
                                        BackgroundColor="Transparent"  
                                        Command="{Binding AddPersonCommand}"  
                                        Grid.Column="1"/>  
                            </Grid>  
                         </VerticalStackLayout>  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.