Building Windows Forms Controls and Components with Rich Design-Time Features

來源:互聯網
上載者:User
Building Windows Forms Controls and Components with Rich Design-Time Features

Michael Weinhardt and Chris Sells 

Design-time Architecture

Design mode is activated the moment a form is opened for editing. It can involve activities like resizing, setting the border style, changing the caption, and adding controls and components, which also enter design mode the moment they are dragged onto either the visual or nonvisual design surface. The nonvisual area, or component tray, hosts components. A component is a Framework class that supports the design-time experience but doesn't draw its own UI in a container-specified region. Components implement the System.ComponentModel.IComponent interface, typically by deriving from the SystemComponent.Model.Component base class. Controls, on the other hand, do draw themselves and are therefore shown in the visual area. All controls ultimately derive from the System.Windows.Forms.Control class, which in turn derives from the Component base class. In other words, a control is really a component with a UI. Both act nearly identically at design time in terms of their integration with the design host. Figure 1 shows a form in design mode acting as a design host for a number of components and controls.

While both components and controls ultimately derive from the Component base class, that's not what distinguishes a component from any other class. Instead, what makes the Component class fit into the design-time architecture is its implementation of the IComponent interface. IComponent allows components to integrate with the design-time host, providing them with design-time services.

設計時架構

   一旦表單被開啟編輯,設計模式就被啟用了。它可以接受很多行為,例如,調整大小、設定邊框風格,改變標題,同時也包括添加control(控制項)和Component(組件),當它們被拖拽到可視或非可視地區時,也進入了設計模式。非可視地區,或者稱為組件托盤,用來存放組件。組件是一個Framework類,它支援設計時體驗,但是它不在container(容器)指定的地區繪製自己的UI(使用者介面)。組件實現System.ComponentModel.IComponent介面,更典型的,它繼承自基類System.ComponentModel.Component。另一方面,控制項卻(do)繪製自己,因此它們可以顯示在可視地區。所有控制項最終都繼承自System.Windows.Forms.Control類,而後者又繼承自Component基類。換句話說,控制項實際上是帶有UI的組件。在設計時期間,二者與設計宿主結合的方式幾乎完全一致。圖1顯示了一個在設計模式下做為許多組件和控制項的設計宿主的表單。

  既然組件和控制項都最終繼承自Component基類,因此組件與其它類沒有什麼區別。Instead,使Component類適合設計時架構的是它對IComponent介面的實現。IComponent允許組件與設計時宿主結合,並為它們提供設計時服務。

Hosts and Containers

In Visual Studio .NET, the Windows Forms Designer is responsible for providing design-time services during Windows Forms development, including a form's UI and code views. The responsibility of managing integration between design-time objects and the designer is handled by an internal implementation of System.ComponentModel.Design.IDesignerHost. The designer host stores IComponent references to all design-time objects on the current form as well as the form itself, which is also a component. This collection of components is available from the IDesignerHost interface through the property of type System.ComponentModel.IContainer: 

This implementation of IContainer allows the designer host to establish a relationship that helps it manage each of the components placed on the form.

宿主和容器
 
      在Visual Studio .NET中,Windows表單設計器負責在Windows 表單開發期間提供設計時服務,包括表單UI和程式碼檢視。管理設計時對象和設計器整合的任務由一個System.ComponentModel.Design.IDesignerHost介面的內部實現處理。設計器宿主儲存對當前視窗中所有設計時對象的IComponent引用,包括視窗本身,因為它也是一個組件。該組件集合可以通過IDesignerHost介面的Container屬性訪問,該屬性的類型為System.ComponentModel.IContainer:

interface IContainer : IDisposable {
  ComponentCollection Components { get; }
  void Add(IComponent component);
  void Add(IComponent component, string 
      name);
  void Remove(IComponent component);
}

   對IContainer介面的這一實現允許設計器宿主建立關係,從而協助它管理放在表單上每一個組件。

Sites

At design time, contained components can access the designer host, as well as each other, through their container. This two-way relationship is shown in Figure 2. You can also see that the fundamental relationship between the designer host and its components is established with an implementation of the System.ComponentModel.ISite interface: 

  在設計時,被包含的組件和設計器宿主可以通過它們之間的容器互相訪問。這一雙向的關係顯示在圖2中。可以看到,設計器宿主和其組件的基礎關係是通過實現一個System.ComponentModel.ISite介面建立的:interface ISite : IServiceProvider {
  IComponent Component { get; }
  IContainer Container { get; }
  bool DesignMode { get; }
  string Name { get; set; }
}

Internally, a container stores an array of sites. When each component is added to the container, the designer host creates a new site, connecting the component to its design-time container and vice versa, by caching the ISite interface in the IComponent.Site property implementation. 

  在內部,一個容器儲存一個site數組。每當組件被添加到容器時,設計器宿主就建立一個新的site,並通過緩衝IComponent.Site屬性中實現的ISite介面,串連組件和其設計時容器,反之亦然。interface IComponent : IDisposable {
  ISite Site { get; set; }
  event EventHandler Disposed;
}

The Component base class implements IComponent and caches the site's interface in a property. It also provides a helper property to go directly to the component's container without having to go through the site first: 

  組件基類實現IComponent並在屬性中緩衝site的介面。它同時提供一個輔助屬性直接存取組件容器,而不必首先通過site:class Component : MarshalByRefObject, IComponent, IDisposable {
  public IContainer Container { get; }
  public ISite Site { virtual get; virtual set; }
}

The Component base class provides a component with access to both the container and the site directly. A component can also access the Visual Studio .NET designer host by requesting the IDesignerHost interface from the container:

  組件基類提供一個組件,通過它可以直接存取容器和site。組件也可以通過請求容器的IDesignerHost介面訪問Visual Studio .NET的設計器宿主。IDesignerHost designerHost = this.Container as  
    IDesignerHost;

In Visual Studio .NET, the designer has its own implementation of the IDesignerHost interface, but to fit into other designer hosts, it's best for a component to rely only on the interface and not any specific implementation.

  在Visual Studio .NET中,設計器有它自己對IDesignerHost介面的實現,不過為了適應其它的設計器宿主,組件最好只依賴介面而不應依賴於其特定的實現。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.