每当属性值更改时引发事件? [英] Raise an event whenever a property's value changed?

查看:26
本文介绍了每当属性值更改时引发事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个属性,名为ImageFullPath1

There is a property, it's named ImageFullPath1

public string ImageFullPath1 {get; set; }

每当其值发生变化时,我都会触发一个事件.我知道要更改 INotifyPropertyChanged,但我想用事件来做.

I'm going fire an event whenever its value changed. I am aware of changing INotifyPropertyChanged, but I want to do it with events.

推荐答案

INotifyPropertyChanged 接口通过事件实现的.该接口只有一个成员,PropertyChanged,这是一个消费者可以订阅的事件.

The INotifyPropertyChanged interface is implemented with events. The interface has just one member, PropertyChanged, which is an event that consumers can subscribe to.

Richard 发布的版本不安全.下面是如何安全地实现这个接口:

The version that Richard posted is not safe. Here is how to safely implement this interface:

public class MyClass : INotifyPropertyChanged
{
    private string imageFullPath;

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

    protected void OnPropertyChanged(string propertyName)
    {
        OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }

    public string ImageFullPath
    {
        get { return imageFullPath; }
        set
        {
            if (value != imageFullPath)
            {
                imageFullPath = value;
                OnPropertyChanged("ImageFullPath");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

请注意,这会执行以下操作:

Note that this does the following things:

  • 抽象属性更改通知方法,以便您可以轻松地将其应用于其他属性;

  • Abstracts the property-change notification methods so you can easily apply this to other properties;

制作 PropertyChanged 委托的副本尝试调用它之前(如果不这样做会产生竞争条件).

Makes a copy of the PropertyChanged delegate before attempting to invoke it (failing to do this will create a race condition).

正确实现INotifyPropertyChanged接口.

如果您想另外为正在更改的特定属性创建通知,您可以添加以下代码:

If you want to additionally create a notification for a specific property being changed, you can add the following code:

protected void OnImageFullPathChanged(EventArgs e)
{
    EventHandler handler = ImageFullPathChanged;
    if (handler != null)
        handler(this, e);
}

public event EventHandler ImageFullPathChanged;

然后在 OnPropertyChanged("ImageFullPath") 行之后添加 OnImageFullPathChanged(EventArgs.Empty) 行.

Then add the line OnImageFullPathChanged(EventArgs.Empty) after the line OnPropertyChanged("ImageFullPath").

因为我们有 .Net 4.5,所以存在 CallerMemberAttribute,它允许删除源代码中属性名称的硬编码字符串:

Since we have .Net 4.5 there exists the CallerMemberAttribute, which allows to get rid of the hard-coded string for the property name in the source code:

    protected void OnPropertyChanged(
        [System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
    {
        OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }

    public string ImageFullPath
    {
        get { return imageFullPath; }
        set
        {
            if (value != imageFullPath)
            {
                imageFullPath = value;
                OnPropertyChanged();
            }
        }
    }

这篇关于每当属性值更改时引发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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