我没有很多用C#创建用户控件的经验,但是对于要制作的组件我有一个雄心勃勃的想法(对我来说很雄心勃勃,不要@我;))。
我想创建一个仅在设计时向您显示上下文相关属性的控件。我设法通过使用接口和可扩展属性来做到这一点。 (问题来了)。
这是我用来测试的代码的相关部分:
namespace SmartGrid
{
#region MAIN CLASS
[Serializable]
public partial class SmartColumnV2 : UserControl
{
private ColumnType mColumnType;
private Boolean mDesignMode;
private ColumnBase mColumns;
public SmartColumnV2()
{
InitializeComponent();
mDesignMode = DesignMode;
mColumns = new SmartColumnLabel();
}
#region muhPublicProperties
[Category("Column Settings"),
Description("Define the type of cells that the column will host"),
RefreshProperties(RefreshProperties.All)]
public ColumnType ColumnType
{
get
{
return mColumnType;
}
set
{
mColumnType = value;
switch(mColumnType)
{
case ColumnType.LabelCol:
mColumns = new SmartColumnLabel();
break;
case ColumnType.ComboCol:
mColumns = new SmartColumnCombo();
break;
case ColumnType.Spinner:
mColumns = new SmartColumnNumeric();
break;
}
}
}
[Category("Column Settings"),
Description("Element specific and Cell specific Settings"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible | DesignerSerializationVisibility.Content),
TypeConverter(typeof(ExpandableObjectConverter))]
public ColumnBase Columns
{
get
{
return mColumns;
}
set
{
mColumns = value;
}
}
#endregion
}
#endregion
#region MAIN Interface
public interface ColumnBase
{
}
#endregion
#region Smart Label Column
public class SmartColumnLabel : ColumnBase
{
//const string DesignerSerializationSavedSettingsFileName = "smss.dat"; //smart column serialization settings data
private ObservableCollection<Label> mColumnElements;
public SmartColumnLabel()
{
mColumnElements = new ObservableCollection<Label>();
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ObservableCollection<Label> Elements
{
get
{
return mColumnElements;
}
set
{
mColumnElements = value;
}
}
}
#endregion
#region Smart Combo Column
public class SmartColumnCombo : ColumnBase
{
...
}
#endregion
#region Smart Column Spinner
public class SmartColumnNumeric : ColumnBase
{
...
}
#endregion
}
我构建了该组件,然后将该组件放在窗体上进行测试。
在设计时,我在可观察的集合中添加了两个标签,如下所示:
然后检查设计器是否生成了适当的代码,并且确实做到了!
partial class Form1
{
/// <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 Windows Form 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()
{
SmartGrid.SmartColumnLabel smartColumnLabel1 = new SmartGrid.SmartColumnLabel();
this.smartColumnV21 = new SmartGrid.SmartColumnV2();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// smartColumnV21
//
this.smartColumnV21.AutoSize = true;
this.smartColumnV21.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.smartColumnV21.BackColor = System.Drawing.SystemColors.Info;
smartColumnLabel1.Elements.Add(this.label1);
smartColumnLabel1.Elements.Add(this.label2);
this.smartColumnV21.Columns = smartColumnLabel1;
this.smartColumnV21.ColumnType = SmartGrid.V2.SmartTypes.ColumnType.LabelCol;
this.smartColumnV21.Location = new System.Drawing.Point(319, 114);
this.smartColumnV21.MaximumSize = new System.Drawing.Size(150, 0);
this.smartColumnV21.MinimumSize = new System.Drawing.Size(150, 29);
this.smartColumnV21.Name = "smartColumnV21";
this.smartColumnV21.Size = new System.Drawing.Size(150, 29);
this.smartColumnV21.TabIndex = 0;
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 23);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// label2
//
this.label2.Location = new System.Drawing.Point(0, 0);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 23);
this.label2.TabIndex = 0;
this.label2.Text = "label2";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.smartColumnV21);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private SmartGrid.SmartColumnV2 smartColumnV21;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
}
问题#1
为什么要保存该代码,请关闭解决方案并再次将其重新打开,即使自动生成的代码仍然存在,我也看不到我在设计时所做的更改?
如果希望保存并返回,我希望设计器生成的更改能够在设计器中使用,但这似乎失败了,尽管事实上自动生成的代码仍然存在...
问题2 我应该怎么做才能做到这一点?我做错了什么?
我正在使用MS Visual Studio Community 2017