Should weak event listeners be used when listening for DependencyProperty changes?

I searched as part of an implementation of WeakEventManager that listens for changes to DependencyProperties. I am a little confused by the fact that the only weak property change event listener that I find, PropertyChangedEventManager , is intended to be used on types that implement INotifyPropertyChanged.

Does this mean that if you are listening to DependencyProperty for changes

DependencyPropertyDescriptor .FromProperty(target, target.OwnerType) .AddValueChanged(component, handler) 

that I don’t have to worry about leaking instances that survive when events are logged?

+4
source share
1 answer

DependencyPropertyDescriptor leaking for a long time, because of this I had a lot of problems. Unless you explicitly name RemoveValueChanged , all of your components will be RemoveValueChanged . Internally, it supports the HashTable of EventHandler . Here is what he does:

 if (this.valueChangedHandlers == null) this.valueChangedHandlers = new Hashtable(); EventHandler eventHandler = (EventHandler) this.valueChangedHandlers[component]; this.valueChangedHandlers[component] = (object) Delegate.Combine((Delegate) eventHandler, (Delegate) handler); 

Because property descriptors are cached, all of your components will be embedded.

+6
source

All Articles