更新 xamarin Forms 中的类属性

更新 xamarin Forms 中的类属性

问题描述:

我正在使用 xamarin 表单开发移动应用程序,我有一个对象列表.我已经使用这个 OnPropertyChanged 在列表中添加了行并提升属性,并且在保存项目后我想更新对象属性列表的状态.我们如何更新状态属性,这是我的代码示例,请检查代码并更新我,谢谢:-

I am working on mobile app using xamarin forms, I have a list of object. I have added the rows in list and raise property using this OnPropertyChanged and after save the items i want to update the status of list of object property. How we can update Status Property, Here is my code example , please check the code and update me, Thanks:-

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

在您的 Test 类中实施 INotifyPropertyChanged:

Implement INotifyPropertyChanged in your Test class:

    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 会改变这个属性.

And if you have correct binding, after item.Status = true; UI will get change of this property.