views:

801

answers:

3

Silverlight 2 is missing the unload event for a UserControl. Has anyone implemented a workaround for this?

A: 

I think the closest you'll get is Application Services which let you detect when the Silverlight application is closing and respond to it. If you're using a Navigation application then you can hook into the Navigating event of the parent frame to determine that the page is "unloading".

What are you trying to do that requires a UserControl to respond to an unloading event?

Bryant
The usercontrol has background worker threads retrieving data from a web service. These workers need to be closed down in an orderly fashion if the usercontrol is closed.
Phillip Ngan
I'd put those background workers somewhere else and shut them down when you're no longer displaying the user control.
Bryant
+1  A: 

you might want to look here: http://gallery.expression.microsoft.com/en-us/SLUnloadedBehavior

The code is for SL 3 but if you remove the behavior code it should work for Sl2

@KeithMahony

One scenario is that you want to prevent event memory leaks, one of the pitfalls of silverlight development that most people tend to ignore.

Martin Wawrusch
A: 

Consider using Silverlight 4. I comes with - at last - an Unloaded event.

BaBu
But - alas - note that its usefulness is limited. From the doc: "Unloaded is not raised for any object if the Silverlight application shuts down for any reason. This includes cases where the browser host application or out-of-browser window is closed by the user."
BaBu