However, you’ve got to make sure you don’t introduce a memory leak. Now, ordinarily you could use a composite presentation event, which as I mentioned before, can use weak references. But you can’t do that so easily here because you need to respond to the event setting some value so that the browser can show a warning. And because the composite events are dispatched it would be tricky and a bit of a hack to use them. So, I need to implement my own weak event.
Again, this is pretty easy if you have a Weak Collection. The MVVM framework we’re using (Photon) has one built in. I’ll be blogging more about Photon and MVVM soon I hope, time allowing (I had a blow out on the motorway and it appears no tyre places are open Sundays, so train for me!). In the mean time there is some documentation on codeplex so go check it out.
As a pattern I’m going to copy the one used here in the CommandManager (it has a static event, RequerySuggested). It’s a bit smelly because the person subscribing to the event has to make sure they hold a reference to the delegate. You can’t say c.RequerySuggested += DoSomething; because nothing will hold a strong reference. This is described in the remarks in the MSDN documentation. I think it’s good to be consistent with APIs as at least people get what they expect…
And to use it: