Reputation: 43321
When I add IDisposable class member to Windows Forms Form class, I add disposing code to Form's Dispose method. What should I do when I add IDisposable class member to WPF Window class, which is not IDisposable?
Upvotes: 2
Views: 5407
Reputation: 30840
Approaches you can use:
Closed
event on Window
.IDisposable
interface yourself for this Window
.Upvotes: 1
Reputation: 3429
You could implement the IDisposable pattern that hooks into the classes finalizer. This means that your IDisposable member would always get cleared up. The only problem is that you wouldn't know when as it depends on the GC to collect the Window class.
Alternatively you could add an event handler the the Window.Closed
event and do your disposing there.
Upvotes: 0
Reputation: 7953
Extend your window class so that it has IDisposable, then implement the Dispose() method as before:
public class MyWindow : Window, IDisposable
{
public void Dispose()
{
// Dispose your objects here as before.
}
}
Upvotes: 6