【译】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. 从第九章开始,我是边看边译的,没有通读,加之英语 ...
随机推荐
- cassandra安装
从官网下载下来的包解压后有100多M,里面包含了已经编译好的全部程序. 按照方法,进入目录后运行 bin/cassandra -f 运行不成功. 然后根据"https://wiki.apac ...
- hibernate配置文件中的schema="dbo"在MySQL数据库不可用
把项目的数据库由SQL Server更改为MySQL之后,发现hibernate报错. 问题在于schema="dbo",使用SQL Sever数据库时正常,使用MySQL数据库需 ...
- ZOJ3791_An Easy Game
给出两个等长的字符串,每次需要改变m个数字,每次必须改变k个数字,求从第一个串变化到第二个串的方案数. DP.f[i][j]改变i步后,有j个位置被改变的方案数.然后直接枚举当前改变的几个位置是前面重 ...
- Max Sum
Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...
- DEV GridControl双击行事件
首先,需要将gridview1.OptionsBehavior.Editable设为false //双击行弹出nodeDetail信息 private void gridView1_MouseDown ...
- codeIgniter 文件路径的问题
学习CI的时候,发现一个问题,求指点. 整个项目的头部文件共用,CSS.JS 等文件都一起引入.在以下路径下都没问题. 首页 -- http://localhost/citest/index ...
- Tabs - 标签页
<div class="J_TWidget tab" data-widget-type="Tabs"style="width:宽度px; hei ...
- 通过硬编码获取dubbo服务对象
运维进行监控dubbo服务的时候可能会调用dubbo服务对象,并且定期去执行,这时候如果需要添加新的服务,可能需要修改监控dubbo服务的配置,即dubbo-producer.xml或是dubbo-c ...
- Pointers and Dynamic Allocation of Memory
METHOD 1: Consider the case where we do not know the number of elements in each row at compile time, ...
- 「2014-4-13」Think twice before starting the adventure
杂文一篇. 1. 取名字真心是一件特别困难的事情.这位独立开发者花了将近两天的时间,给他的私人项目取了个名字:这篇博客<为何我不鸟你的开源项目>里显然还忽视了一个原因,就是名字取得太烂以至 ...