【问题标题】:ObservableCollection list does not update Listview in Xamarin projectObservableCollection 列表不更新 Xamarin 项目中的 Listview
【发布时间】:2019-03-08 14:59:30
【问题描述】:

我有一个 Xamarin 项目。我想用对象列表填充列表视图,并且列表视图必须在对列表进行任何添加、更新或删除后自行刷新。我已经设法通过添加和更新来做到这一点,但是当我尝试删除 Listview 时不会刷新。我使用 INotifyPropertyChanged 基础模型继承自:

 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); }
        }
}

用适配器填充列表视图:

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 不会刷新。我错过了什么?

【问题讨论】:

  • 您似乎在使用 Xamarin Android? IpManageClass 是什么?
  • 是的,我正在使用 Xamarin Android。 IpManageClass 是保存我用来填充适配器的静态 iplist 的类
  • 您也可以在此处添加该类

标签: listview xamarin observablecollection inotifypropertychanged


【解决方案1】:

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

public void Update()
  {            
      NotifyDataSetChanged();
  }  

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-03
    • 2018-08-14
    • 2018-10-16
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    相关资源
    最近更新 更多