Designer中BeginInit()和EndInit()方法的作用 [英] The role of BeginInit() and EndInit() methods in Designer

查看:21
本文介绍了Designer中BeginInit()和EndInit()方法的作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我弄虚了,Designer使用了 ISupportInitialize 接口的那些方法来支持优化,以确保控件初始化的原子性,并防止在初始化期间对控件进行任何操作.我的问题是:

I've red that those methods of ISupportInitialize interface are used by Designer to support optimization, to ensure atomicity of initialization of controls, and to prevent any action on controls during initialization. My questions are:

  1. 它们以什么方式帮助Designer优化控件的初始化?
  2. 为什么要确保初始化的原子性?
  3. 是否有合理的示例说明何时在Designer不生成的代码中使用它们?

推荐答案

它与优化没有任何关系. ISupportInitialize 是当控件对属性的分配顺序敏感时需要的接口.没有任何方法可以影响设计者分配它们的顺序,而是按字母顺序进行.

It does not have anything to do with optimization. ISupportInitialize is an interface that you need when your control is sensitive to the order in which properties are assigned. There isn't any way to affect the order in which the designer assigns them, it does it alphabetically.

通常,您可以在 BeginInit()方法中将 bool 变量设置为true,在属性设置器中对其进行测试,并且在设置后什么也不做.然后,您的 EndInit()方法使属性值生效.

You typically set a bool variable to true in your BeginInit() method, you test this in the property setters and don't do anything when it is set. Your EndInit() method then makes the property values effective.

您可以在 ErrorProvider组件.注意它如何使用这些方法来推迟数据绑定. PictureBox控件另一个很好的例子,它使用它来推迟图像下载. TrackBar 尚未另一个示例,它使用它来确保 Value 属性在Minimum和Maximum之间.Etcetera,.NET Framework源代码通常是一个很好的地方,可以了解实践中如何使用.NET类型.

You can see a good example of this in the ErrorProvider component. Note how it uses the methods to defer data binding. The PictureBox control is another good example, it uses it to defer image downloading. TrackBar is yet another example, it uses it to ensure that the Value property is between Minimum and Maximum. Etcetera, the .NET Framework source is often a very good place to see how .NET types are used in practice.

这篇关于Designer中BeginInit()和EndInit()方法的作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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