i want that my list view reloard automatically

bilel miled 21 Reputation points
2021-03-01T11:01:23.277+00:00

Hi my problem is i wnat when i add a new row it will be appear directly
i have this model

 namespace hereminder.Models

    {
        [Table("Pins")]
        public class Pins 
        {

            [PrimaryKey,MaxLength(250), Unique]
            public string Label { get; set; }

            [MaxLength(250)]
            public string Todo { get; set; }

            [Unique]
            public double Positionlat { get; set; }
            [Unique]
            public double Positionlon { get; set; }

        }}

and this is the xaml of list view

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="hereminder.listofinterest"
             xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
             BackgroundColor="#B6E13D"
             ios:Page.UseSafeArea="true"
             IconImageSource="pen.png">
    <ContentPage.Content>
        <StackLayout BackgroundColor="#B6E13D">



            <StackLayout  BackgroundColor="White">
                <ListView x:Name="listViewEvent" HasUnevenRows="True" BackgroundColor="White" SeparatorVisibility="Default">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>

                                <StackLayout Margin="5">

                                    <Grid BackgroundColor="WhiteSmoke" ColumnSpacing="0" >
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="auto" />
                                            <ColumnDefinition Width="auto" />
                                        </Grid.ColumnDefinitions>


                                        <Label Grid.Column="1"
                   Grid.Row="0"
                   Text="{Binding Label}"
                   FontSize="20"
                  />

                                        <Label Grid.Column="2"
                   Grid.Row="1"
                   Text="{Binding Todo}" 
                   FontSize="20"
                   />
                                        <ImageButton Grid.Column="2"

                               Command="{Binding Path=BindingContext.UpdateCommand, Source={x:Reference Name=listViewEvent}}"  
                                       CommandParameter="{Binding Label}"
                                                     />

                                    </Grid>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

and this is the class file of listview

namespace hereminder
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class listofinterest 
    {
        public List<Pins> list;
        public ListView lv;

        public listofinterest()
        {
            InitializeComponent();

            list = App.pinsRepo.GetAllPins();


            listViewEvent.ItemsSource = list;



            Padding = new Thickness(0, 20, 0, 0);



        }

    }
}

i need help please

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
4,818 questions
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,846 Reputation points
    2021-03-01T12:45:24.663+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Hi, @bilel miled To update the ListView automatically when new items are added, besides using an ObservableCollection, the model class also needs to implement the INotifyPropertyChanged interface to raise the PropertyChanged event.

    Try to set data binding using MVVM pattern to update the data at runtime, check the code:

       public partial class TestPage : ContentPage  
       {  
           TestViewModel viewModel;  
           public TestPage()  
           {  
               InitializeComponent();  
         
               viewModel = new TestViewModel();  
               BindingContext = viewModel;  
           }  
       }  
         
       public class TestModel : INotifyPropertyChanged  
       {  
           private string theContent;  
           public string TheContent  
           {  
               get  
               {  
                   return theContent;  
               }  
               set  
               {  
                   if (theContent != value)  
                   {  
                       theContent = value;  
                       NotifyPropertyChanged("TheContent");  
                   }  
               }  
           }  
         
           protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")  
           {  
               PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
           }  
         
           public event PropertyChangedEventHandler PropertyChanged;  
       }  
         
       public class TestViewModel  
       {  
           public ObservableCollection<TestModel> ContentCollection { get; set; }  
           public TestViewModel()  
           {  
               ContentCollection = new ObservableCollection<TestModel>();  
               //add the data  
           }  
       }  
    

    Set data binding for the listView in xaml

       <ListView x:Name="listview" ItemsSource="{Binding DataCollection}">  
           <ListView.ItemTemplate>  
               <DataTemplate>  
                   <ViewCell>  
                       <StackLayout>  
                           <Label Text="{Binding TheContent}"/>  
                       </StackLayout>  
                   </ViewCell>  
               </DataTemplate>  
           </ListView.ItemTemplate>  
       </ListView>  
    

    Related doc: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/data-bindings-to-mvvm

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    No comments