BT

Weak Events in WPF 4.5

by Jonathan Allen on Feb 16, 2012 |

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.

Hello stranger!

You need to Register an InfoQ account or 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

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Simple WeakEvent Implemention by Onoue Masanori

An implemention of WeakEvent pattern for WPF/Silverlight/Windows Phone.
this is simpler and more intuitive than traditional ones.

ugaya40.net/codememo/an-implemention-of-weakeve...

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

1 Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT