Xamarin form update listView itemSource

后端 未结 6 947
闹比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:42

    You can define a base view model and inherit it from INotifyPropertyChanged

    public abstract class BaseViewModel : INotifyPropertyChanged
        {
            protected bool ChangeAndNotify(ref T property, T value, [CallerMemberName] string propertyName = "")
            {
                if (!EqualityComparer.Default.Equals(property, value))
                {
                    property = value;
                    NotifyPropertyChanged(propertyName);
                    return true;
                }
    
    
                return false;
            }
    
    
            protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
        }
    

    then in your viewmodel (Ex. JM) will be inherit from BaseViewModel and can create ObservableCollection List

    Also your fields in ViewModel (Ex. JM) should implement like following:

    public const string FirstNamePropertyName = "FirstName";
    private string firstName = string.Empty;
    public string FirstName 
    {
        get { return firstName; }
        set { this.ChangeAndNotify(ref this.firstName, value, FirstNamePropertyName); }
    } 
    

    Hope this helps.

提交回复
热议问题