【问题标题】:Update class property in xamarin Forms更新 xamarin Forms 中的类属性
【发布时间】:2017-01-13 22:53:05
【问题描述】:

我正在使用 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");
            }
        }
    }

【问题讨论】:

    标签: c# mvvm xamarin.forms


    【解决方案1】:

    在您的 Test 类中实现 INotifyPropertyChanged

        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 之后会得到这个属性的改变。

    【讨论】:

    • ,是的,我已经在 BaseViewModel 中实现了 INotifyPropertyChanged。这样我就使用此方法更新了属性 ** OnPropertyChanged("Status");** 但没有更新状态。
    • TestBaseViewModel 是不同的类。从视图模型中,您可以更新放置在视图模型中的属性。您应该在 Test 类中实现 INotifyPropertyChanged 以从 Test 更新 Status
    • 感谢@Egor Gromadskiy
    猜你喜欢
    • 2017-11-25
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多