How to deinitialize a FrameworkElement?

2.4k views Asked by At

We have a class which derives from FrameworkElement and which is not under our control. This class registers eventhandlers in the OnInitialize method.

What is the pattern to properly clean this class up, since FrameworkElement does not provide a counterpart to OnInitialize?

It remains rooted since no part of it removes the EventHandler causing a leak.

3

There are 3 answers

3
Panagiotis Kanavos On BEST ANSWER

There's no such thing as "deinitializing". Initialization methods are used when it isn't possible to fully initialize an object in its constructor, because it depends on data that isn't available during construction. In these cases construction is broken in two phases: the first step is executed in a parameterless constructor, the second in an initialization method, like OnInitialize, after the external data becomes available

What you describe is object disposal, which is performed by calling an object's Dispose method. A well-written class should clean up its data, release any external resources and release any event handlers.

Visual elements typically have another step in their lifecycle, handled by the OnLoad/OnUnload methods. The Load step occurs when an element is actually placed in XAML view and connected to the other UI elements. Event handlers should be registered in the OnLoad method and removed in the OnUnload method.

If the element has no Dispose method , you may be able to raise the Unload event to force the cleanup, although this is a bit of a hack:

control.RaiseEvent(new RoutedEventArgs(FrameworkElement.UloadedEvent))
0
Dennis On

Use weak event pattern. In particular, subscribe to events using WeakEventManager implementations (or, better, its generic version WeakEventManager<TEventSource, TEventArgs>, if .NET 4.5 is an option).

0
Cluster On

Use Dispatcher.ShutdownStarted event.

Add this to the constructor:

Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;

And add your cleanup code to this method:

private void Dispatcher_ShutdownStarted(object sender, EventArgs e)
{
    Stop();
}