组件设计者如何知道在InitializeComponent中要初始化哪些控件值 [英] How does the component designer know which control values to intitialize in InitializeComponent
问题描述
在创建自定义控件(UserControl)时,IDE为您的< usercontrol> .cs提供标准的设计器< usercontrol> .Designer.cs.我试图了解设计器如何知道在InitializeComponent()中初始化哪些类成员.当我将事件处理程序分配给event时,Designer中的哪种机制可以完成此任务.我有一个字符串成员propert文本,在设计时未更新.
这当然是< usercontrol> .Designer.cs
When creating a custom control (UserControl) the IDE gives you a standard Designer <usercontrol>.Designer.cs for your <usercontrol>.cs. I''m trying to understand how the designer knows which class members to initialize in InitializeComponent(). When I assign an event handler to and event what mechanism in Designer accomplishes this. I have a string member propert Text that is not being updated at design time.
this of course is the <usercontrol>.Designer.cs
#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()
{
this.SuspendLayout();
//
// SW
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.Name = "SW";
this.Size = new System.Drawing.Size(39, 42);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.BitSW_Paint);
this.Resize += new System.EventHandler(this.BitSW_Resize);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.BitSW_MouseUp);
this.ResumeLayout(false);
}
UserControl.Text被覆盖
the UserControl.Text is overridden
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[Description("Text label for this control.")]
public override string Text
{
get
{
return base.Text;
}
set
{
if (value != base.Text)
{
base.Text = value;
Invalidate();
}
}
}
谢谢大家,加尔斯(gals)帮了大忙.
Thanks guys and gals any help is greatly.
推荐答案
所有带有setter的公共属性都将序列化到设计器文件,只要它们没有用
All public properties that have a setter are serialized to the designer file so long as they aren''t decorated with
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
如果属性具有默认值(通过DefaultValue
属性或ShouldSerializePropertyName
和ResetPropertyName
),则如果其当前值==默认值,则该属性不会被序列化.
If a property has a default value (by either DefaultValue
attribute or ShouldSerializePropertyName
and ResetPropertyName
) then it won''t be serialized if it''s current value == the default value.
如果转到UserControl的定义,则会看到Text
属性是这样的:
If you goto the definition for UserControl you will see that the Text
property is like this:
[Bindable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override string Text { get; set; }
在您的替代中,执行此操作
In your override, do this
[EditorBrowsable(EditorBrowsableState.Advanced)]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get { return base.Text; }
set
{
if (value != base.Text)
{
base.Text = value;
Invalidate();
}
}
}
this.sw1.AlignText = System.Drawing.ContentAlignment.MiddleCenter;
this.sw1.BackColor = System.Drawing.Color.Black;
this.sw1.BackOff = System.Drawing.Color.Black;
this.sw1.BackOn = System.Drawing.Color.Lime;
this.sw1.bIsOn = false;
this.sw1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.sw1.ForeColor = System.Drawing.Color.Lime;
this.sw1.ForeOff = System.Drawing.Color.Lime;
this.sw1.ForeOn = System.Drawing.Color.Black;
this.sw1.Location = new System.Drawing.Point(137, 81);
this.sw1.Name = "sw1";
this.sw1.Size = new System.Drawing.Size(20, 74);
this.sw1.TabIndex = 0;
由于没有Text属性的初始化程序,因此发生了一些变化.现在,我确实覆盖了UserControl.Text属性
Something has changed then because there is no initializer for the Text property. Now I did override the UserControl.Text property
/// <summary>
/// Text label for this control."
/// </summary>
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[Description("Text label for this control.")]
public override string Text
{
get
{
return base.Text;
}
set
{
if (value != base.Text)
{
base.Text = value;
Invalidate();
}
}
}
这篇关于组件设计者如何知道在InitializeComponent中要初始化哪些控件值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!