IDisposable member of WPF Window class

自作多情 提交于 2019-12-06 04:45:03

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.
    }
}

Approaches you can use:

  • Use Closed event on Window.
  • Implement IDisposable interface yourself for this Window.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!