有几种方法可以在VS中开始创建新的自定义控件?我应该使用哪一个?如:
Right click project -> Add ->
1. User Control, or
2. Component, or
3. Custom Control, or?
我使用#1并获得类似于:
的部分类namespace MyControls
{
partial class NewControl
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose ( bool disposing )
{
if ( disposing && ( components != null ) )
{
components.Dispose ( );
}
base.Dispose ( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent ( )
{
components = new System.ComponentModel.Container ( );
//this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
}
#endregion
}
为什么我需要这门课?它只对需要使用设计器的复合控件有用吗?
编辑:我知道部分关键字和类,但我不知道为什么我需要这个特定的类来进行自定义控件。它似乎没有什么区别。
答案 0 :(得分:4)
部分类允许您将自定义代码添加到类中,而无需修改IDE生成的文件。这可以避免在IDE发现需要更新其生成的内容时调整IDE生成的文件以使您的更改被破坏的常见问题。
答案 1 :(得分:4)
此类包含InitializeComponent
方法,该方法由设计器生成,并包含创建设计器所需的所有代码。
如果您根本不使用组件设计器,则可以删除此文件(以及ResX文件,如果有),并从类声明中删除partial
关键字。
请注意,删除此文件会永久销毁您在设计器中所做的任何事情。
答案 2 :(得分:2)
除了Michael Burr所说的内容之外,这有助于隐藏设计器实现,因此您不太可能进行更改,导致设计人员无法解析您的文件。
恕我直言,他们还通过隐藏仅用于实现控件GUI的混乱来帮助保持代码文件的清洁。您可以专注于使控件按您希望的方式工作的其他细节。答案 3 :(得分:0)
它允许设计人员拥有一个可以随意进行更改的整个文件,而不必担心会损坏您的代码。我怀疑这样可以更容易地实现设计师,这样就不必在jasonh评论中提到的那些评论和“跳舞”。
答案 4 :(得分:-1)
是的,它适用于您希望使用设计器来创建控件的时间。就像winforms一样,您希望自定义代码避免在设计器重新生成.designer.cs文件时丢失。