更好的PropertyChanged和PropertyChanging事件处理 [英] Better PropertyChanged and PropertyChanging event handling

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

问题描述

我正在为我们的应用程序实现观察者模式 - 目前正在使用RX框架。



我现在有一个示例,如下所示:

  Observable.FromEventPattern< PropertyChangedEventArgs>(Instance.Address,PropertyChanged)
.Where(e => e.EventArgs。 PropertyName ==City)
.ObserveOn(Scheduler.ThreadPool)
.Subscribe(search => OnNewSearch(search.EventArgs));

(我有一个类似的PropertyChanging)



EventArgs不给我太多。我想要的是一个EventArgs的扩展,它将使我能够看到前面的和新的值,以及在更改侦听器中标记事件的能力,以使更改实际上不会持续。如何做到这一点?感谢。

解决方案

我认为这是由你如何实现INotifyPropertyChanging和INotifyPropertyChanged接口。



PropertyChangingEventArgs和PropertyChangedEventArgs类不提供属性的前后值或者取消更改的能力,但是你可以派生自己的事件args类来提供这个功能。 / p>

首先,定义以下事件args类。请注意,这些派生自PropertyChangingEventArgs类和PropertyChangedEventArgs类。这允许我们将这些对象作为参数传递给PropertyChangingEventHandler和PropertyChangedEventHandler委托。

 类PropertyChangingCancelEventArgs:PropertyChangingEventArgs 
{
public bool取消{get;组; }

public PropertyChangingCancelEventArgs(string propertyName)
:base(propertyName)
{
}
}

class PropertyChangingCancelEventArgs& T> :PropertyChangingCancelEventArgs
{
public T OriginalValue {get;私人集}

public T NewValue {get;私人集}

public PropertyChangingCancelEventArgs(string propertyName,T originalValue,T newValue)
:base(propertyName)
{
this.OriginalValue = originalValue;
this.NewValue = newValue;
}
}

class PropertyChangedEventArgs< T> :PropertyChangedEventArgs
{
public T PreviousValue {get;私人集}

public T CurrentValue {get;私人集}

public PropertyChangedEventArgs(string propertyName,T previousValue,T currentValue)
:base(propertyName)
{
this.PreviousValue = previousValue;
this.CurrentValue = currentValue;
}
}

接下来,您需要在实现INotifyPropertyChanging和INotifyPropertyChanged接口。一个实现的例子如下:

  class示例:INotifyPropertyChanging,INotifyPropertyChanged 
{
public event PropertyChangingEventHandler属性

public event PropertyChangedEventHandler PropertyChanged;

protected bool OnPropertyChanging< T>(string propertyName,T originalValue,T newValue)
{
var handler = this.PropertyChanging;
if(handler!= null)
{
var args = new PropertyChangingCancelEventArgs< T>(propertyName,originalValue,newValue);
handler(this,args);
return!args.Cancel;
}
return true;
}

protected void OnPropertyChanged< T>(string propertyName,T previousValue,T currentValue)
{
var handler = this.PropertyChanged;
if(handler!= null)
handler(this,new PropertyChangedEventArgs< T>(propertyName,previousValue,currentValue));
}

int _ExampleValue;

public int ExampleValue
{
get {return _ExampleValue; }
set
{
if(_ExampleValue!= value)
{
if(this.OnPropertyChanging(ExampleValue,_ExampleValue,value))
{
var previousValue = _ExampleValue;
_ExampleValue = value;
this.OnPropertyChanged(ExampleValue,previousValue,value);
}
}
}
}
}


$ b b

注意,PropertyChanging和PropertyChanged事件的事件处理程序仍然需要将原始的PropertyChangingEventArgs类和PropertyChangedEventArgs类作为参数,而不是更具体的版本。但是,您可以将事件args对象转换为更具体的类型,以访问新的属性。



下面是这些事件的事件处理程序的示例:

  class Program 
{
static void Main(string [] args)
{
var exampleObject = new Example();

exampleObject.PropertyChanging + = new PropertyChangingEventHandler(exampleObject_PropertyChanging);
exampleObject.PropertyChanged + = new PropertyChangedEventHandler(exampleObject_PropertyChanged);

exampleObject.ExampleValue = 123;
exampleObject.ExampleValue = 100;
}

static void exampleObject_PropertyChanging(object sender,PropertyChangingEventArgs e)
{
if(e.PropertyName ==ExampleValue)
{
int originalValue =((PropertyChangingCancelEventArgs< int>)e).OriginalValue;
int newValue =((PropertyChangingCancelEventArgs< int>)e).NewValue;

//如果新值小于原始值,则不允许更改属性
if(newValue< originalValue)
((PropertyChangingCancelEventArgs)e)。 Cancel = true;
}

}

static void exampleObject_PropertyChanged(object sender,PropertyChangedEventArgs e)
{
if(e.PropertyName ==ExampleValue )
{
int previousValue =((PropertyChangedEventArgs< int>)e).PreviousValue;
int currentValue =((PropertyChangedEventArgs< int>)e).CurrentValue;
}
}
}


I am implementing the observer pattern for our application - currently playing around with the RX Framework.

I currently have an example that looks like this:

Observable.FromEventPattern<PropertyChangedEventArgs>(Instance.Address, "PropertyChanged")
    .Where(e => e.EventArgs.PropertyName == "City")
    .ObserveOn(Scheduler.ThreadPool)
    .Subscribe(search => OnNewSearch(search.EventArgs));

(I have a similar one for "PropertyChanging")

The EventArgs don't give me much. What I would like is an extension of the EventArgs that would give me the ability to see the previous and new values, as well as the ability to mark the event in the 'changing' listener, such that the change wouldn't actually persist. How can this be done? Thanks.

解决方案

I think that it comes down to how you implement the INotifyPropertyChanging and INotifyPropertyChanged interfaces.

The PropertyChangingEventArgs and PropertyChangedEventArgs classes unfortunately don't provide a before and after value of the property or the ability to cancel the change, but you can derive your own event args classes that do provide that functionality.

First, define the following event args classes. Notice that these derive from the PropertyChangingEventArgs class and PropertyChangedEventArgs class. This allows us to pass these objects as arguments to the PropertyChangingEventHandler and PropertyChangedEventHandler delegates.

class PropertyChangingCancelEventArgs : PropertyChangingEventArgs
{
    public bool Cancel { get; set; }

    public PropertyChangingCancelEventArgs(string propertyName)
        : base(propertyName)
    {
    }
}

class PropertyChangingCancelEventArgs<T> : PropertyChangingCancelEventArgs
{
    public T OriginalValue { get; private set; }

    public T NewValue { get; private set; }

    public PropertyChangingCancelEventArgs(string propertyName, T originalValue, T newValue)
        : base(propertyName)
    {
        this.OriginalValue = originalValue;
        this.NewValue = newValue;
    }
}

class PropertyChangedEventArgs<T> : PropertyChangedEventArgs
{
    public T PreviousValue { get; private set; }

    public T CurrentValue { get; private set; }

    public PropertyChangedEventArgs(string propertyName, T previousValue, T currentValue)
        : base(propertyName)
    {
        this.PreviousValue = previousValue;
        this.CurrentValue = currentValue;
    }
}

Next, you would need to use these classes in your implementation of the INotifyPropertyChanging and INotifyPropertyChanged interfaces. An example of an implementation is the following:

class Example : INotifyPropertyChanging, INotifyPropertyChanged
{
    public event PropertyChangingEventHandler PropertyChanging;

    public event PropertyChangedEventHandler PropertyChanged;

    protected bool OnPropertyChanging<T>(string propertyName, T originalValue, T newValue)
    {
        var handler = this.PropertyChanging;
        if (handler != null)
        {
            var args = new PropertyChangingCancelEventArgs<T>(propertyName, originalValue, newValue);
            handler(this, args);
            return !args.Cancel;
        }
        return true;
    }

    protected void OnPropertyChanged<T>(string propertyName, T previousValue, T currentValue)
    {
        var handler = this.PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs<T>(propertyName, previousValue, currentValue));
    }

    int _ExampleValue;

    public int ExampleValue
    {
        get { return _ExampleValue; }
        set
        {
            if (_ExampleValue != value)
            {
                if (this.OnPropertyChanging("ExampleValue", _ExampleValue, value))
                {
                    var previousValue = _ExampleValue;
                    _ExampleValue = value;
                    this.OnPropertyChanged("ExampleValue", previousValue, value);
                }
            }
        }
    }
}

Note, your event handlers for the PropertyChanging and PropertyChanged events will still need to take the original PropertyChangingEventArgs class and PropertyChangedEventArgs class as parameters, rather than a more specific version. However, you will be able to cast the event args objects to your more specific types in order to access the new properties.

Below is an example of event handlers for these events:

class Program
{
    static void Main(string[] args)
    {
        var exampleObject = new Example();

        exampleObject.PropertyChanging += new PropertyChangingEventHandler(exampleObject_PropertyChanging);
        exampleObject.PropertyChanged += new PropertyChangedEventHandler(exampleObject_PropertyChanged);

        exampleObject.ExampleValue = 123;
        exampleObject.ExampleValue = 100;
    }

    static void exampleObject_PropertyChanging(object sender, PropertyChangingEventArgs e)
    {
        if (e.PropertyName == "ExampleValue")
        {
            int originalValue = ((PropertyChangingCancelEventArgs<int>)e).OriginalValue;
            int newValue = ((PropertyChangingCancelEventArgs<int>)e).NewValue;

            // do not allow the property to be changed if the new value is less than the original value
            if(newValue < originalValue)
                ((PropertyChangingCancelEventArgs)e).Cancel = true;
        }

    }

    static void exampleObject_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "ExampleValue")
        {
            int previousValue = ((PropertyChangedEventArgs<int>)e).PreviousValue;
            int currentValue = ((PropertyChangedEventArgs<int>)e).CurrentValue;
        }
    }
}

这篇关于更好的PropertyChanged和PropertyChanging事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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