后,我称此功能为此
我有一个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不会刷新。我想念什么?
我找到了解决方案的工作。我在适配器上添加了更新功能:
public void Update()
{
NotifyDataSetChanged();
}
删除列表的任何对象