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.
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:
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: