Xamarin form update listView itemSource

后端 未结 6 913
闹比i
闹比i 2021-01-04 18:28

Ok I have a ListView object which have a List as ItemSource and I\'d like to refresh the ItemSource whene

6条回答
  •  旧巷少年郎
    2021-01-04 18:55

    Here is my current implementation of this pattern extracted from an app I'm working on, as concisely as I could make it.

    
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    
    namespace MyNamespace
    {
        // This base view model takes care of implementing INotifyPropertyChanged
        // In your extended View Model classes, use SetValue in your setters.
        // This will take care of notifying your ObservableCollection and hence
        // updating your UI bound to that collection when your view models change.
        public abstract class BaseViewModel : INotifyPropertyChanged
        {
            protected void SetValue(ref T backingField, T value, [CallerMemberName] string propertyName = null)
            {
                if (EqualityComparer.Default.Equals(backingField, value)) return;
                backingField = value;
                OnPropertyChanged(propertyName);
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            private void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
        // Using MvvM, this would likely be a View Model class.
        // However, it could also be a simple POCO model class
        public class MyListItem : BaseViewModel
        {
            private string _itemLabel = "List Item Label";
            public string Label
            {
                get => _itemLabel;
                set => SetValue(ref _itemLabel, value);
            }
        }
    
        // This is your MvvM View Model
        // This would typically be your BindingContext on your Page that includes your List View
        public class MyViewModel : BaseViewModel
        {
            private ObservableCollection _myListItemCollection
                = new ObservableCollection();
    
            public ObservableCollection MyListItemCollection
            {
                get { return _myListItemCollection; }
                set => SetValue(ref _myListItemCollection, value);
            }
        }
    
    }
    
    
    
    

提交回复
热议问题