Updating ObservableCollection in CollectionView

Aston13 41 Reputation points
2021-05-01T03:24:54.097+00:00

Hi, I am using CollectionView to display a list of ObservableCollection<BindableItems>, the objects are updated with any changes every 15 seconds through a HTTP GET request.

93028-image.png

However, when updating the Object properties in the ObservableCollection, the changes were not updated in the UI.

As a solution, I made all the properties inside the BindableItems use a SetProperty function from MvvmHelpers which inherits from INotifyPropertyChanged.

Then when I reassign the properties of the existing BindableItem objects, the UI is updated too.

92959-image.png

92899-image.png

I am new to Xamarin and I am used to working with Vue where reactivity is mostly taken care of for you. I wonder if my approach/solution is correct especially from a performance standpoint as I am updating a list of 100 or more objects which have 10 properties each every 15 seconds. Is this the recommended way? :)

An alternative I can think of would be to just clear the list and reinsert new objects but that seems wasteful.

Thanks

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,971 Reputation points
    2021-05-03T01:59:04.503+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    An alternative I can think of would be to just clear the list and reinsert new objects but that seems wasteful.

    After creating a ViewModel class and setting BindingContext for the page, please make sure the data collection you want to update is from the bindingContext.

    Check the following code:

       public partial class TestPage : ContentPage  
       {  
           TestPageViewModel viewModel;  
           public TestPage()  
           {  
               InitializeComponent();  
         
               viewModel = new TestPageViewModel();  
               BindingContext = viewModel;  
           }  
         
           private async void Button_Clicked(object sender, EventArgs e)  
           {  
               //update the data  
               viewModel.BindableItems.Add(new BindableItem{ ... });  
           }  
       }  
         
       public class TestPageViewModel  
       {  
           public ObservableCollection<BindableItem> BindableItems { get; set; }  
           public TestPageViewModel()  
           {  
               DataCollection = new ObservableCollection<BindableItem>();  
               //add the data  
           }  
       }  
    

    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.


0 additional answers

Sort by: Most helpful

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.