每当属性值更改时引发一个事件?

有一个属性,它名为ImageFullPath1


public string ImageFullPath1 {get; set; }

每当值改变时,我都会触发一个事件。我知道更改INotifyPropertyChanged,但是我想通过事件来更改。


BIG阳
浏览 642回答 3
3回答

慕码人8056858

该INotifyPropertyChanged接口是与事件实现。该界面只有一个成员,PropertyChanged这是消费者可以订阅的事件。理查德发布的版本不安全。以下是安全实现此接口的方法: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;}请注意,这将执行以下操作:抽象属性更改通知方法,以便您可以轻松地将其应用于其他属性;在尝试调用PropertyChanged代理之前,先制作该代理的副本(否则,将创建竞争条件)。正确实现INotifyPropertyChanged接口。如果要为更改的特定属性另外创建通知,则可以添加以下代码:protected void OnImageFullPathChanged(EventArgs e){    EventHandler handler = ImageFullPathChanged;    if (handler != null)        handler(this, e);}public event EventHandler ImageFullPathChanged;然后在该行OnImageFullPathChanged(EventArgs.Empty)之后添加该行OnPropertyChanged("ImageFullPath")。由于我们有.Net 4.5,因此存在CallerMemberAttribute,它可以摆脱源代码中属性名称的硬编码字符串:    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();            }        }    }

缥缈止盈

我使用与Aaronaught大致相同的模式,但是如果您有很多属性,最好使用一些通用方法魔术使代码更干燥。public class TheClass : INotifyPropertyChanged {&nbsp; &nbsp; private int _property1;&nbsp; &nbsp; private string _property2;&nbsp; &nbsp; private double _property3;&nbsp; &nbsp; protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) {&nbsp; &nbsp; &nbsp; &nbsp; PropertyChangedEventHandler handler = PropertyChanged;&nbsp; &nbsp; &nbsp; &nbsp; if(handler != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handler(this, e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; protected void SetPropertyField<T>(string propertyName, ref T field, T newValue) {&nbsp; &nbsp; &nbsp; &nbsp; if(!EqualityComparer<T>.Default.Equals(field, newValue)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field = newValue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OnPropertyChanged(new PropertyChangedEventArgs(propertyName));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public int Property1 {&nbsp; &nbsp; &nbsp; &nbsp; get { return _property1; }&nbsp; &nbsp; &nbsp; &nbsp; set { SetPropertyField("Property1", ref _property1, value); }&nbsp; &nbsp; }&nbsp; &nbsp; public string Property2 {&nbsp; &nbsp; &nbsp; &nbsp; get { return _property2; }&nbsp; &nbsp; &nbsp; &nbsp; set { SetPropertyField("Property2", ref _property2, value); }&nbsp; &nbsp; }&nbsp; &nbsp; public double Property3 {&nbsp; &nbsp; &nbsp; &nbsp; get { return _property3; }&nbsp; &nbsp; &nbsp; &nbsp; set { SetPropertyField("Property3", ref _property3, value); }&nbsp; &nbsp; }&nbsp; &nbsp; #region INotifyPropertyChanged Members&nbsp; &nbsp; public event PropertyChangedEventHandler PropertyChanged;&nbsp; &nbsp; #endregion}通常,我还将OnPropertyChanged方法设为虚拟,以允许子类覆盖它以捕获属性更改。

12345678_0001

INotifyPropertyChanged正是在属性更改时引发事件。要实现INotifyPropertyChanged,需要一个成员,即PropertyChanged事件。您自己实现的任何内容都可能与该实现相同,因此不使用它没有任何好处。
打开App,查看更多内容
随时随地看视频慕课网APP