为什么WPF不使用IDisposable,其后果是什么? [英] Why does WPF not use IDisposable, and what are the ramifications?

查看:226
本文介绍了为什么WPF不使用IDisposable,其后果是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,到目前为止,我从来没有做过一个相当大的WPF项目,但是我想知道这一点.没有实现IDisposable的WPF类怎么办?相比之下,Windows窗体中的所有UI元素均实现IDisposable,以确保它们摆脱了底层句柄等.

So, I've never done a sizable WPF project before now, but this one has me wondering; What's up with WPF classes not implementing IDisposable? By comparison, all of the UI elements in Windows Forms implement IDisposable, to assure they get rid of the underlying handles and such.

我认为相同的Windows对象在其中,因此必须释放这些资源.那么,WPF怎么做到的?

I think the same Windows objects are under the covers there, and those resources have to be released; so, how is WPF doing that?

除了Close()之外,我还需要对WPF Window对象做些什么吗?

Is there anything I need to be doing with my WPF Window objects beyond Close()ing them?

推荐答案

WinForms控件具有句柄,因为它们是Win32控件的包装. WPF控件不是(嗯,窗口不是,但它们托管的控件不是).原因是,毕竟,WPF窗口只是Direct3D渲染上下文,所有控件只是一堆三角形.因此,它们不需要实际注册到操作系统,因此,它们没有句柄(除了窗口和从HwndHost继承的任何东西,当然,这些 Win32对象) ).

WinForms controls have handles because they are wrappers around Win32 controls. WPF controls are not (well, windows are, but the controls they host are not). The reason is that, after all, a WPF window is just a Direct3D rendering context, and all controls are just a bunch of triangles. So, they don't need to be actually registered to the OS, and therefore, they don't have handles (except windows and anything that inherits from HwndHost, of course, which are Win32 objects).

这就是为什么WPF和WinForms之间有如此大的互操作层的原因:WPF控件根本不是Windows对象.

That's why there's such a sizeable interop layer between WPF and WinForms: WPF controls simply aren't Windows objects.

这篇关于为什么WPF不使用IDisposable,其后果是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆