什么是"Control.Site"属性?

What is the "Control.Site" Property?

在 Windows Forms 应用程序中,System.Windows.Forms 库中的控件有一个名为 Site 的 属性。 属性 在 Controls 中的工作是什么?

站点绑定 Component to a Container 并启用它们之间的通信,并为容器提供一种管理其组件的方法。 站点还可以作为容器特定的、每个组件的信息(例如组件名称)的存储库。有关组件的详细信息,请参阅 Programming with Components

Notes to Implementers

To be a site, a class must implement the ISite interface.

参考: https://msdn.microsoft.com/en-us/library/system.componentmodel.isite.aspx

Site属性继承自Component,非常像ControlParent属性。

ParentSite的主要区别是Parent的值只能是Control,而Site可以有non-visual 分配给它的容器。

Component base-class 用于 Winforms 设计器工具箱中的那些 non-visual 工具。例如 System.Windows.Forms.Timer 可以拖到 Form 上。 PropertyGrid 可用于设置其属性并分配 event-handlers,所有这些都来自设计者,无需编写一行代码。

System.ComponentModel 类 背后的想法是为软件库提供 API 以利用 IDE 的 design-time 功能,例如Visual Studio。它迎合了 RAD (Rapid Application Development) concepts where general-purpose or generic components would take advantage of the API. For example to expose extra information about a property in the bottom section of the property-grid, or even create complete custom editors.

如果您想深入了解内部结构,您可以查看 Programming with Components, or if you want a quick overview, I guess Class vs. Component vs. Control 可能是一个很好的起点。