什么叫INotifyPropertyChanged的的PropertyChanged事件的最好方法是什么? [英] What's the best way to call INotifyPropertyChanged's PropertyChanged event?

查看:141
本文介绍了什么叫INotifyPropertyChanged的的PropertyChanged事件的最好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实现INotifyPropertyChanged接口,你是负责调用PropertyChanged事件每每次一个属性在类更新。

When you implement the INotifyPropertyChanged interface, you're responsible for calling the PropertyChanged event each and everytime a property is updated in the class.

这通常会导致以下code:

This typically leads to the following code :

    public class MyClass: INotifyPropertyChanged

        private bool myfield;
        public bool MyField
        {
            get { return myfield; }
            set
            {
                if (myfield == value)
                    return;
                myfield = value;
                OnPropertyChanged(new PropertyChangedEventArgs("MyField"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler h = PropertyChanged;
            if (h != null)
                h(this, e);
        }
   }

这是 12行每个属性。

这将是非常简单的,如果一个人能装点这样的自动属性:

It would be so much simpler if one was able to decorate automatic properties like this :

[INotifyProperty]
public double MyField{ get; set; }

但不幸的是,这是不可能的(见这个帖子 MSDN上的例如)

But unfortunately this is not possible (see this post on msdn for example)

我怎样才能减少每财产所需的code量?

How can I reduce the amount of code needed per property?

推荐答案

其实,这是只有3-4行的每个属性的;其他线的摊销的对所有通知属性:

Actually, it's only 3-4 lines per property; the other lines are amortized over all "notifying" properties:

class Person : INotifyPropertyChanged
{
    #region INotifyPropertyChanged: Shared bit
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }
    #endregion

    private string _firstName;
    public string FirstName
    {
        get { return _firstName; }
        set
        {
            if (_firstName == value)
                return;
            _firstName = value;
            OnPropertyChanged(new PropertyChangedEventArgs("FirstName"));
        }
    }

    // Ditto for other properties
}

您可以尝试像下面这股更多的负载:

You could try something like the following, which shares some more of the load:

private string _firstName;
public string FirstName
{
    get { return _firstName; }
    set { SetNotifyingProperty("FirstName", ref _firstName, value); }
}
private void SetNotifyingProperty<T>(string propertyName,
                                     ref T field, T value)
{
    if (value.Equals(field))
        return;
    field = value;
    OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}

这篇关于什么叫INotifyPropertyChanged的的PropertyChanged事件的最好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆