How to unsubscribe from events

Feb 7, 2010 at 4:01 PM
Edited Feb 7, 2010 at 4:03 PM


in my application there is a service which publishes events and a item which subscribes to this event.

The item is a view, base upon your FormStackCS example.

When the view is closed/not visible it should not handle this event.

Is there a way to unsubscribe from events?

Kind regards,


Feb 9, 2010 at 2:53 PM

If you're using event aggregation (via EventSubscription) then no, there's no easy way to unsubscribe.  You would have to walk the framework collections looking for items that publish and then unhook them via reflection, and that really defeats the "set it and forget it" nature of the framework.

If you want to not handle the events in a consumer you need to eitehr keep track of state and exit out of the handler based on logic or you have to manually wire in the events and not use aggregation.

In all, though, I think you have an architecture flaw.  In a real-world app your views should not be subscribing to events from the model.  A Presenter/Controller should be.  The view should get its info from that Presenter (or be set by the Presenter) so the Presenter would have the event handler and then pass it on to the View only if the View's Visible state is true.