每当物业价值发生变化时提高活动? [英] Raise an event whenever a property's value changed?

查看:98
本文介绍了每当物业价值发生变化时提高活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个属性,它被命名为 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.

理查德贴不安全。以下是如何安全地实现这个界面:

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;

然后添加行 OnImageFullPathChanged(EventArgs.Empty) after the line OnPropertyChanged(ImageFullPath)

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

由于我们有.Net 4.5, a href =https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.callermembernameattribute.aspx =noreferrer> 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天全站免登陆