【译】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但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利!

ISupportInitialize的用处的更多相关文章

  1. 【译】ISupportInitialize的用处

    [译]ISupportInitialize的用处 注:本文是对How ISupportInitialize Can Help的翻译.原文作者编写了Sharpgl,这篇文章是对制作Winform控件过程 ...

  2. BIT祝威博客汇总(Blog Index)

    +BIT祝威+悄悄在此留下版了个权的信息说: 关于硬件(Hardware) <穿越计算机的迷雾>笔记 继电器是如何成为CPU的(1) 继电器是如何成为CPU的(2) 关于操作系统(Oper ...

  3. 【repost】document.write的用处

    document.write的用处 document.write是JavaScript中对document.open所开启的文档流(document stream操作的API方法,它能够直接在文档流中 ...

  4. 【转】代码中特殊的注释技术——TODO、FIXME和XXX的用处

    (转自:http://blog.csdn.net/reille/article/details/7161942) 作者:reille 本博客网址:http://blog.csdn.net/reille ...

  5. 使用angular中ng-repeat , track by的用处

    我们见到最简单的例子是: <div ng-repeat="link in links" ></div> 如果item的值有重复的,比如links=[&quo ...

  6. github的pull request是指什么意思?有什么用处

    github的pull request是指什么意思? 来看看某乎某位阿牛的理解,多么的简单粗暴! 我尝试用类比的方法来解释一下 pull reqeust.想想我们中学考试,老师改卷的场景吧.你做的试卷 ...

  7. Docker对普通开发者的用处(转)

    有些开发者可能还是不明白 Docker 对自己到底有多大的用处,因此翻译 Docker 个人用例 这篇文章中来介绍 Docker 在普通开发者开发过程中的用例. Docker 如今赢得了许多关注,很多 ...

  8. java中的反射机制在Android开发中的用处

    JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反 ...

  9. 懒加载的用处和赋nil操作[iOS开发教程]

    懒加载的用处和赋nil操作 1:数据,清空操作: self.array = nil; 2:归档从新从本地获取数据 self.archive = nil; ##id = nil的用处 block当参数, ...

随机推荐

  1. DDD分层架构的进化

    .NET逻辑分层架构演示:DDD分层架构的进化 概述:   架构是高层的设计,如果设计和理解有误,必将在实现时带来各种问题.架构又是最稳定的,不会因为各种具体技术的依赖,如各种UI框架.ORM框架.I ...

  2. likely()与unlikely()

    he gcc C compiler has a built-in directive that optimizes conditional branches as either very likely ...

  3. C# 程序自动批量生成 google maps 的KML文件

    原文:C# 程序自动批量生成 google maps 的KML文件 google maps 的 KML 文件可以用于静态的地图标注,在某些应用中,我们手上往往有成百上千个地址,我们需要把这些地址和描述 ...

  4. linux 在系统启动过程

    从学习<鸟哥linux私人厨房> 用于在计算机系统启动,计算机硬件和软件由(它包含的操作系统软件)包括.对于操作系统在同一台计算机硬件方面的表现,该系统配备有硬件是公用,不同的系统是 的操 ...

  5. Cf 444C DZY Loves Colors(段树)

    DZY loves colors, and he enjoys painting. On a colorful day, DZY gets a colorful ribbon, which consi ...

  6. 轻量级IOC框架Guice

    java轻量级IOC框架Guice Guice是由Google大牛Bob lee开发的一款绝对轻量级的java IoC容器.其优势在于: 速度快,号称比spring快100倍. 无外部配置(如需要使用 ...

  7. 《C语言及程序设计初步》网络课程主页

    题记 CSDN要开在线教育频道,向我发出邀请,看能否开些课程. 我近日一直在关注着翻转课堂,试图在传统课堂中引入新的元素,这须要资源建设的积累.没有时间表的工作,非常难把握. 为CSDN做在线课程,为 ...

  8. ”Validation of viewstate MAC failed” 错误

    ”Validation of viewstate MAC failed” 错误 在ASP.NET里面,View State使用较为广泛.它作为一个隐藏字段,可以帮助服务端”记住“客户端的改变,这样客户 ...

  9. FPGA 设计怎样进行面积优化(逻辑资源占用量优化)

    FPGA面积优化 1 对于速度要求不是非常高的情况下,我们能够把流水线设计成迭代的形式,从而反复利用FPGA功能同样的资源. 2 对于控制逻辑小于共享逻辑时,控制逻辑资源能够用来复用,比如FIR滤波器 ...

  10. Spring之IOC容器加载初始化的方式

    引言 我们知道IOC容器时Spring的核心,可是如果我们要依赖IOC容器对我们的Bean进行管理,那么我们就需要告诉IOC容易他需要管理哪些Bean而且这些Bean有什么要求,这些工作就是通过通过配 ...