我正在使用xamarin表单处理移动应用程序,我有一个对象列表。我已经在列表中添加了行并使用此 OnPropertyChanged 来提升属性,并在保存项目之后我想更新对象属性列表的状态。我们如何更新状态属性,这是我的代码示例,请检查代码并更新我,谢谢: -
class Test
{
public int ID{ get; set; }
public string Name { get; set; }
public bool Status { get; set; }
}
class Consume : BaseViewModel
{
void main()
{
ObservableCollection<Test> coll = new ObservableCollection<Test>();
coll = await db.GetData();
foreach (var item in coll)
{
item.Status = true;
//How we can update Status property of class
OnPropertyChanged("Status");
}
}
}
答案 0 :(得分:1)
在INotifyPropertyChanged
课程中实施Test
:
class Test : INotifyPropertyChanged
{
public int ID { get; set; }
public string Name { get; set; }
private bool _status;
public bool Status
{
get { return _status; }
set
{
_status = value;
RaisePropertyChanged();
}
}
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged([CallerMemberName]string propertyName = "")
{
Volatile.Read(ref PropertyChanged)?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
如果你有正确的绑定,在item.Status = true;
UI之后将更改此属性。