制作Windows窗体控件只读了IDisposable [英] Making Windows Forms controls read-only and IDisposable

查看:226
本文介绍了制作Windows窗体控件只读了IDisposable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使Windows只读窗体控件和IDisposable的。

I want to make Windows Forms controls readonly and IDisposable.

这是好事还是坏事和我有什么要注意的时候调用Dispose?

Is this is a good thing or a bad thing and what do I have to watch out for when calling Dispose?

我有一个选项卡(PageTab),我伸出并插入其中有一个列表视图,另一个控制工具栏到面板。这些选项卡,然后插入到了一个标签控件(所有本机.NET Windows窗体控件)。

I have a Tab (PageTab) that I extend and insert a Panel which has a listview and another Toolbar control into. These tabs then get inserted into a tab control (all native .NET Windows Forms controls).

当用户关闭这些选项卡我调用Dispose方法(其中一个如下的 MSDN 方式实现IDisposable接口)的。

When the user closes one of these tabs I call the Dispose method (which follows the MSDN way of implementing IDisposable).

是否明智或建议的申报控件只读(见下文)?

Is it wise or suggested to declare the controls as read-only (see below)?

protected readonly ListView _AccountsList = new ListView();
protected readonly Panel _Panel = new Panel();

由于在Dispose方法我只是叫 _Panel.Dipose(),等他们,但我不能将它们设置为null。我想避免泄漏,就像我可以和有事情垃圾收集。

Because in the Dispose method I just call _Panel.Dipose(), etc. on them, but I cannot set them to null. I want to avoid leaks as much as I can and have things garbage collected.

什么是一个非GUI设计师发展的最佳途径,处置他们?

What's the best way for a non-Designer GUI development and disposing them?

推荐答案

Control.Dispose的默认实现(通过继承的TabPage)是相当足够。它的迭代子控件存储在控件集合成员,并调用其Dispose()方法。您没有帮助。

The default implementation of Control.Dispose (inherited by TabPage) is quite sufficient. It iterates the child controls as stored in the Controls collection member and calls their Dispose() method. You don't have to help.

有只有两种情况下,你应该调用Dispose()明确:

There are only two cases where you should call Dispose() explicitly:


  • 当您从Controls集合的控件。这里调用Dispose()是一个硬性要求。不这样做将保持在窗口办理的延续,同时计划的生活,这是不可见的。您将有泄漏,如果你不处理它。

  • 当您使用显示的ShowDialog形式()。这绕过了正常的自动处置的形式和它的子控件的,一定让你可以不用冒着一个ObjectDisposed例外读回对话的结果。 using语句做到这一点。
  • 的正确方法

后一种情况实际上并不是发生泄漏,控制类的终结确保该对话框的窗口句柄最终被释放,假设有留下来的对话框对象没有活引用。

The latter case is not in fact a leak, the Control class' finalizer ensures that the window handle for the dialog eventually is released, assuming there are no live references left to the dialog object.

控制就是忘记调用Dispose极少数类之一()实际上可以导致不可控的资源泄漏。你是,你必须调用Dispose()明确您的TabPage派生对象处置它,当你删除的页面正确的。但你不必担心它的子控件。

Control is one of the very few classes where forgetting to call Dispose() can in fact cause an uncontrollable resource leak. You are correct in that you have to call Dispose() explicitly on your TabPage derived object to dispose it when you remove the page. But you don't have to worry about its child controls.

这篇关于制作Windows窗体控件只读了IDisposable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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