ObservableCollection列表不会更新Xamarin项目中的Listview

问题描述 投票:0回答:1

我有一个Xamarin项目。我想用列表对象填充列表视图,列表视图必须在添加,更新或删除列表后自行刷新。我已设法使用添加和更新,但当我尝试删除Listview不刷新。我使用INotifyPropertyChanged basemodel继承自:

 public class BaseModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;


        protected bool SetProperty<T>(ref T storage, T value,
            [CallerMemberName] String propertyName = null)
        {
            if (object.Equals(storage, value)) return false;

            storage = value;
            this.OnPropertyChanged(propertyName);
            return true;
        }

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var eventHandler = this.PropertyChanged;
            if (eventHandler != null)
            {
                eventHandler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }

ipsettings模型:

public class IpSettingModel : BaseModel
    {
        public IpSettingModel(String ip)
        {
            this._ip = ip;            
        }

        private string _ip = string.Empty;        
        public string ip
        {
            get { return this._ip; }
            set { this.SetProperty(ref this._ip, value); }
        }
}

使用适配器填充listview:

ObservableCollection<IpSettingModel> iplist = new ObservableCollection<IpSettingModel>();

    IpAdapter myadapter = new IpAdapter(this, iplist);
    ipListview.Adapter = myadapter;

到现在为止还挺好。如果我添加或更新列表的对象,它工作正常。当我尝试删除一个对象

IpManageClass.datasource.Remove(IpManageClass.datasource.Where(o => o.ip == selecteditem.ip).Single());

该对象将从ObservableCollection中删除,但Listview不会刷新。我错过了什么?

listview xamarin observablecollection inotifypropertychanged
1个回答
0
投票

我找到了解决方案。我在适配器上添加了更新功能:

public void Update()
  {            
      NotifyDataSetChanged();
  }  

我删除列表中的任何对象后调用此函数

© www.soinside.com 2019 - 2024. All rights reserved.