【译】ISupportInitialize的用处

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

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

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

问题

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

 //
// 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 = ;
this.openGLControl1.DrawRenderTime = true;
this.openGLControl1.FrameRate = 29.41176F;
this.openGLControl1.Location = new System.Drawing.Point(, );
this.openGLControl1.Name = "openGLControl1";
this.openGLControl1.RenderContextType = SharpGL.RenderContextType.NativeWindow;
this.openGLControl1.Size = new System.Drawing.Size(, );
this.openGLControl1.TabIndex = ;
this.openGLControl1.OpenGLDraw += new System.Windows.Forms.PaintEventHandler(this.openGLControl1_OpenGLDraw);

于是问题来了。

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

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

 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();
}

好了。想让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 ISupportInitilized can help

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

【译】ISupportInitialize的用处的更多相关文章

  1. ISupportInitialize的用处

      [译]ISupportInitialize的用处   [译]ISupportInitialize的用处 注:本文是对How ISupportInitialize Can Help的翻译.原文作者编 ...

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

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

  3. [译+改]最长回文子串(Longest Palindromic Substring) Part II

    [译+改]最长回文子串(Longest Palindromic Substring) Part II 原文链接在http://leetcode.com/2011/11/longest-palindro ...

  4. [译] 反思 1 号进程 / Rethinking PID 1

    By Lennart Poettering 译 SReadFox 原文链接:http://0pointer.de/blog/projects/systemd.html 译注:笔者大约在 2011 年读 ...

  5. [译] PEP 255--简单的生成器

    我正打算写写 Python 的生成器,然而查资料时发现,引入生成器的 PEP 没人翻译过,因此就花了点时间翻译出来.如果在阅读时,你有读不懂的地方,不用怀疑,极有可能是我译得不到位.若出现这种情况,我 ...

  6. [译]The Python Tutorial#4. More Control Flow Tools

    [译]The Python Tutorial#More Control Flow Tools 除了刚才介绍的while语句之外,Python也从其他语言借鉴了其他流程控制语句,并做了相应改变. 4.1 ...

  7. [译] 重新思考 1 号进程 / Rethinking PID 1

    By Lennart Poettering 译 SReadFox 原文链接:http://0pointer.de/blog/projects/systemd.html 译注:笔者大约在 2011 年读 ...

  8. RxJS + Redux + React = Amazing!(译一)

    今天,我将Youtube上的<RxJS + Redux + React = Amazing!>翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: https:/ ...

  9. Entity Framework 6 Recipes 2nd Edition 译 -> 目录 -持续更新

    因为看了<Entity Framework 6 Recipes 2nd Edition>这本书前面8章的翻译,感谢china_fucan. 从第九章开始,我是边看边译的,没有通读,加之英语 ...

随机推荐

  1. 删除所选项(附加搜索部分的jquery)

    1.视图端(views)的配置为: <script> $(document).ready(function() { $("#info-grid").kendoGrid( ...

  2. python:threading多线程模块-创建线程

    创建线程的两种方法: 1,直接调用threading.Thread来构造thread对象,Thread的参数如下: class threading.Thread(group=None, target= ...

  3. k次出现与一次出现的数字

    原始的题目是这样的: Single Number II Given an array of integers, every element appears three times except for ...

  4. 基于ArcGIS Viewer for Flex开发的一款跨平台的应用程序

    特点: 1.基于ArcGIS Viewer for Flex开发的一款跨平台的应用程序: -(IBAction) showTOC:(id)sender { if (_tocViewController ...

  5. 三大基础排序算法BubbleSort、SelectSort、InsertSort

    public class Strategy { public static void main(String[] args) { int [] array=new int[]{26,25,15,42, ...

  6. testlink简单部署

    CentOS+LAMP+testlink 环境 系统 CentOS6.5 软件 testlink-1.9.14 IP 192.168.0.158 部署 LAMP环境搭建 remi配置 wget htt ...

  7. Python 学习---------Day3

    第七章 字符串单双引号字符串是一样的用转义序列代表特殊字节字符串抑制转义myfile=open(r'C:\new\text.dat','w')三重引号编写多行字符串块字符串更大的编码集std(u'sp ...

  8. 初学python第二天

    今天我将用python来编写一款小游戏,用这个来总结一下自己学过的一些基础语法.没错,它就是井字游戏.想想自己第一接触这种游戏,还是小学生呢

  9. Codeforces #380 div2 B(729B) Spotlights

    B. Spotlights time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  10. 转载:JProfiler远程监控LINUX上的Tomcat过程细讲

    来源于xuwanbest的博客   所谓"工欲善其事,必先利其器",好的工具确能起到事半工倍的作用.我用到的最多的就两个JConsole 和JProfiler .JConsole监 ...