ISupportInitialize的用处

【译】ISupportInitialize的用处

 

【译】ISupportInitialize的用处

注:本文是对How ISupportInitialize Can Help的翻译。原文作者编写了Sharpgl,这篇文章是对制作Winform控件过程中的一个知识点的小总结。我只按照理解的需要简单翻译一下,仅供参考。

我最近才发现ISupportInitialize这个接口。在开发复杂一点的winform控件的时候它实在是很有用。

MSDN上有对ISupportInitialize的介绍,我这里只说一下在什么情况下用它发挥作用。

问题

我要做一个比较复杂的控件“OpenGLControl”,它能够在winform程序中执行opengl命令,渲染出3D场景。这个控件有一些相关的属性,在设计器里,这些属性是这样写(自动生成)的:

复制代码
 1 // 
 2 // openGLControl1
 3 // 
 4 this.openGLControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
 5                 | System.Windows.Forms.AnchorStyles.Left)
 6                 | System.Windows.Forms.AnchorStyles.Right)));
 7 this.openGLControl1.BitDepth = 32;
 8 this.openGLControl1.DrawRenderTime = true;
 9 this.openGLControl1.FrameRate = 29.41176F;
10 this.openGLControl1.Location = new System.Drawing.Point(12, 12);
11 this.openGLControl1.Name = "openGLControl1";
12 this.openGLControl1.RenderContextType = SharpGL.RenderContextType.NativeWindow;
13 this.openGLControl1.Size = new System.Drawing.Size(768, 379);
14 this.openGLControl1.TabIndex = 0;
15 this.openGLControl1.OpenGLDraw += new System.Windows.Forms.PaintEventHandler(this.openGLControl1_OpenGLDraw);
复制代码

于是问题来了。

BitDepth,OpenGLDraw,FrameRate等等必须在Size这个属性之前设置好。这我们怎么控制?这种自动生成的代码天晓得我们怎么去管它。

于是ISupportInitialize接口出场了。如果OpenGLControl实现了这个接口,那么在设计器里自动生成的代码就会是这样的:

复制代码
 1 private void InitializeComponent()
 2 {
 3     System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormExample1));
 4     this.label1 = new System.Windows.Forms.Label();
 5     this.linkLabel1 = new System.Windows.Forms.LinkLabel();
 6     this.openGLControl1 = new SharpGL.OpenGLControl();
 7     ((System.ComponentModel.ISupportInitialize)(this.openGLControl1)).BeginInit();
 8     this.SuspendLayout();
 9     //
10     //  ...ordianry designer code...
11     //
12     ((System.ComponentModel.ISupportInitialize)(this.openGLControl1)).EndInit();
13     this.ResumeLayout(false);
14     this.PerformLayout();
15 }
复制代码

好了。想让Size属性最后设置是吧,好说啊,在EndInit方法里设置它就好了。

PS:原文如下:


How ISupportInitialize Can Help Leave a reply I have recently come to discover the ISupportInitialize interface and found that it is extremely useful when developing more complicated WinForms controls. Here’s the link to the ISupportInitialize interface on MSDN: http://msdn.microsoft.com/en-us/library/system.componentmodel.isupportinitialize.aspx but here I’ll describe how it can be useful. The Problem I have a fairly complicated WinForms usercontrol called ‘OpenGLControl’, which allows OpenGL commands to be used to render 3D scenes in a C# WinForms application. The control has properties which are interdependent to each other. If these properties are set in the designer, code like this is generated: // // openGLControl1 // this.openGLControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.openGLControl1.BitDepth = 32; this.openGLControl1.DrawRenderTime = true; this.openGLControl1.FrameRate = 29.41176F; this.openGLControl1.Location = new System.Drawing.Point(12, 12); this.openGLControl1.Name = "openGLControl1"; this.openGLControl1.RenderContextType = SharpGL.RenderContextType.NativeWindow; this.openGLControl1.Size = new System.Drawing.Size(768, 379); this.openGLControl1.TabIndex = 0; this.openGLControl1.OpenGLDraw += new System.Windows.Forms.PaintEventHandler(this.openGLControl1_OpenGLDraw);Now this leads to a problem – BitDepth, OpenGLDraw, FrameRate etc must all be declared BEFORE the Size property is set – but how can we control this? Or how can we deal with this situation in general? This is where the ISupportInitialize interface comes in. If a control is added to the design surface with this interface, we’ll get the following code wrapped around the designer code: private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormExample1)); this.label1 = new System.Windows.Forms.Label(); this.linkLabel1 = new System.Windows.Forms.LinkLabel(); this.openGLControl1 = new SharpGL.OpenGLControl(); ((System.ComponentModel.ISupportInitialize)(this.openGLControl1)).BeginInit(); this.SuspendLayout(); // // ...ordianry designer code... // ((System.ComponentModel.ISupportInitialize)(this.openGLControl1)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); }Now just implement the ISupportInitialize interface in your control – in the ‘EndInit’ function do any processing that depends on the interdependent properties. This is the earliest point that we can do processing like this. In certain circumstances, knowing about this interface can save you a lot of trouble. This entry was posted in News and tagged C# on September 18, 2011 by Dave Kerr.
复制代码
How ISupportInitialize Can Help
Leave a reply 
I have recently come to discover the ISupportInitialize interface and found that it is extremely useful when developing more complicated WinForms controls.

Here’s the link to the ISupportInitialize interface on MSDN: http://msdn.microsoft.com/en-us/library/system.componentmodel.isupportinitialize.aspx but here I’ll describe how it can be useful.

The Problem

I have a fairly complicated WinForms usercontrol called ‘OpenGLControl’, which allows OpenGL commands to be used to render 3D scenes in a C# WinForms application. The control has properties which are interdependent to each other. If these properties are set in the designer, code like this is generated:

// 
// openGLControl1
// 
this.openGLControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                | System.Windows.Forms.AnchorStyles.Left)
                | System.Windows.Forms.AnchorStyles.Right)));
this.openGLControl1.BitDepth = 32;
this.openGLControl1.DrawRenderTime = true;
this.openGLControl1.FrameRate = 29.41176F;
this.openGLControl1.Location = new System.Drawing.Point(12, 12);
this.openGLControl1.Name = "openGLControl1";
this.openGLControl1.RenderContextType = SharpGL.RenderContextType.NativeWindow;
this.openGLControl1.Size = new System.Drawing.Size(768, 379);
this.openGLControl1.TabIndex = 0;
this.openGLControl1.OpenGLDraw += new System.Windows.Forms.PaintEventHandler(this.openGLControl1_OpenGLDraw);Now this leads to a problem – BitDepth, OpenGLDraw, FrameRate etc must all be declared BEFORE the Size property is set – but how can we control this? Or how can we deal with this situation in general?

This is where the ISupportInitialize interface comes in. If a control is added to the design surface with this interface, we’ll get the following code wrapped around the designer code:

private void InitializeComponent()
{
    System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormExample1));
    this.label1 = new System.Windows.Forms.Label();
    this.linkLabel1 = new System.Windows.Forms.LinkLabel();
    this.openGLControl1 = new SharpGL.OpenGLControl();
    ((System.ComponentModel.ISupportInitialize)(this.openGLControl1)).BeginInit();
    this.SuspendLayout();
    //
    //  ...ordianry designer code...
    //
    ((System.ComponentModel.ISupportInitialize)(this.openGLControl1)).EndInit();
    this.ResumeLayout(false);
    this.PerformLayout();
}Now just implement the ISupportInitialize interface in your control – in the ‘EndInit’ function do any processing that depends on the interdependent properties. This is the earliest point that we can do processing like this. In certain circumstances, knowing about this interface can save you a lot of trouble.

This entry was posted in News and tagged C# on September 18, 2011 by Dave Kerr.
复制代码

本文由BIT祝威撰写,转载请注明出处http://www.cnblogs.com/bitzhuwei但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利!

原文地址:https://www.cnblogs.com/Leo_wl/p/3179859.html