IDisposable member of WPF Window class

孤街浪徒 提交于 2019-12-07 23:58:20

问题


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?


回答1:


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



回答2:


Approaches you can use:

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



回答3:


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.



来源:https://stackoverflow.com/questions/4450904/idisposable-member-of-wpf-window-class

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