【译】ISupportInitialize的用处
【译】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的用处的更多相关文章
- ISupportInitialize的用处
[译]ISupportInitialize的用处 [译]ISupportInitialize的用处 注:本文是对How ISupportInitialize Can Help的翻译.原文作者编 ...
- BIT祝威博客汇总(Blog Index)
+BIT祝威+悄悄在此留下版了个权的信息说: 关于硬件(Hardware) <穿越计算机的迷雾>笔记 继电器是如何成为CPU的(1) 继电器是如何成为CPU的(2) 关于操作系统(Oper ...
- [译+改]最长回文子串(Longest Palindromic Substring) Part II
[译+改]最长回文子串(Longest Palindromic Substring) Part II 原文链接在http://leetcode.com/2011/11/longest-palindro ...
- [译] 反思 1 号进程 / Rethinking PID 1
By Lennart Poettering 译 SReadFox 原文链接:http://0pointer.de/blog/projects/systemd.html 译注:笔者大约在 2011 年读 ...
- [译] PEP 255--简单的生成器
我正打算写写 Python 的生成器,然而查资料时发现,引入生成器的 PEP 没人翻译过,因此就花了点时间翻译出来.如果在阅读时,你有读不懂的地方,不用怀疑,极有可能是我译得不到位.若出现这种情况,我 ...
- [译]The Python Tutorial#4. More Control Flow Tools
[译]The Python Tutorial#More Control Flow Tools 除了刚才介绍的while语句之外,Python也从其他语言借鉴了其他流程控制语句,并做了相应改变. 4.1 ...
- [译] 重新思考 1 号进程 / Rethinking PID 1
By Lennart Poettering 译 SReadFox 原文链接:http://0pointer.de/blog/projects/systemd.html 译注:笔者大约在 2011 年读 ...
- RxJS + Redux + React = Amazing!(译一)
今天,我将Youtube上的<RxJS + Redux + React = Amazing!>翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: https:/ ...
- Entity Framework 6 Recipes 2nd Edition 译 -> 目录 -持续更新
因为看了<Entity Framework 6 Recipes 2nd Edition>这本书前面8章的翻译,感谢china_fucan. 从第九章开始,我是边看边译的,没有通读,加之英语 ...
随机推荐
- MYSQL使用mysqldump导出某个表的部分数据
命令格式如下: mysqldump -u用户名 -p密码 数据库名 表名 --where="筛选条件" > 导出文件路径 例子: 从meteo数据库的sdata表中导出sen ...
- NGUI Material Shader SetFloat 不起作用
通常情况下,我们在Unity3d种可以通过material.SetFloat(name, xxx) 修改参数来达到我们的Material[Shader]的效果. 但是在 NGUI 的UITexture ...
- AspNetPager 免费分页控件7.5.1版发布!
AspNetPager 免费分页控件7.5.1版发布,本次升级主要内容有: 修正了ShowDisabledButtons为false时html闭合标签丢失的bug:改为从System.Web.UI.W ...
- javaweb中实现在线人数统计
session并不是浏览器关闭时销毁的,而是在session失效的时候销毁下列代码就是监测session创建.销毁 package com.my.count; import javax.servlet ...
- C# JS 单例
单例模式的三个特点: 1,该类只有一个实例 2,该类自行创建该实例(在该类内部创建自身的实例对象) 3,向整个系统公开这个实例接口 模式1: class Singleton { //私有,静态的类自身 ...
- C++builder中使用TScrollBox 以后,让scrollBox相应鼠标的上下滑动
1.在窗口的事件里搜索 mouseWheel的方法 2.在.cpp文件里实现下面的代码 void __fastcall TForm1::mouseWheel(TObject *Sender, TShi ...
- MATLAB 秒表函数 tic toc 计算程序运行时间
若需要测试出程序运行所需时间,或对不同的运行方式所需时间进行对比,则可利用秒表函数tic和toc.Tic函数启动定时器,第一个紧跟它的toc函数终止定时器并报告此时定时器的流逝时间.其语法如下: t ...
- 如何删除选中的checkbox
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- MySQL数据库“局部”乱码
问题:“网页显示中午”与“数据库查看中文”总有一个是乱码,或者“网页中总有部分中文乱码” 装了PHPStudy之后,用alter修改过一次数据库的编码方式为utf8!当时的网页的编码显示是正常的,所以 ...
- (转) silverlight 样式学习
原文地址:http://www.cnblogs.com/Joetao/articles/2074727.html <UserControl x:Class="StyleDemo.Mai ...