程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> ISupportInitialize接口的用處

ISupportInitialize接口的用處

編輯:關於.NET

我最近才發現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 = 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);

於是問題來了。

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

出處http://www.cnblogs.com/bitzhuwei

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved