Weak Events in WPF 4.5
Weak Events have always been an important memory management technique for the style data binding used in WPF and Silverlight. The former even went so far as to provide a WeakEventManager base class that can be built upon. Unfortunately the weak event pattern is rather onerous for both the event source and event handler. Not only must a custom subclass of WeakEventManager be created for each event type, the event handler needs to implement a matching listener interface. This can be particularly frustrating when you need to monitor the same event on several objects with a different hander for each.
With .NET 4.5 this becomes much simpler. A generic version of WeakEventManager is being provided for WPF developers. In addition to not needing the event-specific subclass, this version also dispenses with the need for listener interfaces. Internally the library uses reflection to actually wire up the event, so performance isn’t quite as good as the alternatives.
This new approach is not without limitations. Like the original, this new weak event manager isn’t available in Silverlight which in turn makes code sharing harder. It also continues to be bound to the dispatcher thread, making it unusable non-WPF applications. Finally, the event is referenced by name rather than by
InfoQ invites you to post your own favorite weak event implementation in the comments.
Simple WeakEvent Implemention
by
Onoue Masanori
this is simpler and more intuitive than traditional ones.
ugaya40.net/codememo/an-implemention-of-weakeve...
Educational Content
Concurrency in Clojure
Stuart Halloway May 17, 2013
Confessions of an Agile Addict
Ole Friis Østergaard May 16, 2013
Web Development: You're Doing It Wrong
Stefan Tilkov May 16, 2013
Programming The Feynman Way
Ben Evans May 15, 2013





Hello stranger!
You need to Register an InfoQ account or Login to post comments. But there's so much more behind being registered.Get the most out of the InfoQ experience.
Tell us what you think