BT

Your opinion matters! Please fill in the InfoQ Survey!

Weak Events in WPF 4.5

| by Jonathan Allen Follow 254 Followers on Feb 16, 2012. Estimated reading time: 1 minute |

A note to our readers: As per your request we have developed a set of features that allow you to reduce the noise, while not losing sight of anything that is important. Get email and web notifications by choosing the topics you are interested in.

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.

Rate this Article

Adoption Stage
Style

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

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT