BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Weak Events in WPF 4.5

Weak Events in WPF 4.5

This item in japanese

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
Style

BT