Cleanup for control inside a FlowDocument

Posted by Thorarin on Stack Overflow See other posts from Stack Overflow or by Thorarin
Published on 2010-04-05T12:26:53Z Indexed on 2010/04/05 12:33 UTC
Read the original article Hit count: 399

Filed under:
|
|
|
|

I have a custom control that I use inside a FlowDocument. The control uses a System.Drawing.ImageAnimator to display transparent, animated GIF images. Why is this such a pain in the butt in WPF anyway? :P

In my original implementation, this was causing memory leaks when a paragraph containing the control was being deleted from the document, because the ImageAnimator kept a reference to the control for event handling.

I've now implemented a WeakEventManager pattern which seems to indeed fix the leak itself, but I would like to stop "OnFrameChanged" events from being fired if a particular animated GIF is not currently in the document, instead of relying on the garbage collector to eventually collect the control objects and my event manager to notice that there no longer are valid listeners to the event.

Basically, I would like to take a more active role in this and have the control react to being removed from the FlowDocument. Is there some way to do this? I've been unable to find it. OnVisualParentChanged doesn't get fired, because the direct parent (a Paragraph) is unchanged.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about wpf-controls