Figuring out when a silverlight user control is being disposed
How can I tell if my silverlight user control is being disposed / destroyed?
Normally I can use the destructor, but I have come across a situation where there is a memory leak in a telerik control. It prevents the destructor being called.开发者_高级运维
There is a control event called 'Unloaded' but my control is on a tab. When the tab is changed the 'Unloaded' event fires.
I have a work around for the memory leak, but I need to catch the fact that the control is in the process of being destroyed and execute my 'work around'.
Any ideas?
I don't know details of your project, but maybe you can handle Unloaded event from tab. In this case you can be sure tab within all child controls is unloaded from the current object tree. Also you can handle Application.Exit, in case you run your code in out-of-browser process you can use Window.Closing event (available in Silverlight 4 or later).
The question is do you really need to know when to the UserControl is destroyed or disposed? If you build up and tear down your control (event handlers, children, etc.) in the Loaded and Unloaded events, then you should be able to correctly handle when controls become invisible (disconnected from visual tree), as well as when the control is being removed for good. We used this approach in our last Silverlight 4 application that allows you to design organizations; everything from organizational capabilities down to the equipment needed. The application has a lot of views coming and going, so we needed to make sure that everything was unhooked properly.
精彩评论