WPF Window 类的 IDisposable 成员
当我将 IDisposable 类成员添加到 Windows 窗体 Form 类时,我将处置代码添加到 Form 的 Dispose 方法中。当我将 IDisposable 类成员添加到 WPF Window 类(不是 IDisposable)时,该怎么办?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
扩展您的窗口类,使其具有 IDisposable,然后像以前一样实现 Dispose() 方法:
Extend your window class so that it has IDisposable, then implement the Dispose() method as before:
您可以使用的方法:
Window
上使用Closed
事件。Window
实现IDisposable
接口。Approaches you can use:
Closed
event onWindow
.IDisposable
interface yourself for thisWindow
.您可以实现挂接到类终结器中的 IDisposable 模式。这意味着您的 IDisposable 成员总是会被清除。唯一的问题是你不知道什么时候,因为它取决于 GC 来收集 Window 类。
或者,您可以向 Window.Closed 事件添加一个事件处理程序,并在那里进行处理。
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.