c# – 用户控件自定义属性在构建时丢失值
发布时间:2020-12-16 01:55:52 所属栏目:百科 来源:网络整理
导读:我有一个名为“UserControl1”的UserControl,里面有一个标签和一个自定义属性: [Browsable(true)]public new string Text{ get { return label1.Text; } set { label1.Text = value; }}public UserControl1(){ InitializeComponent();} 此UserControl用于名
我有一个名为“UserControl1”的UserControl,里面有一个标签和一个自定义属性:
[Browsable(true)] public new string Text { get { return label1.Text; } set { label1.Text = value; } } public UserControl1() { InitializeComponent(); } 此UserControl用于名为“Form1”的表单中. 如果我将属性名称更改为其他单词,则一切正常.请注意“new”关键字以覆盖基础变量.. 我找到了类似的问题here,但没有解决方案. 问候! 编辑:没有硬编码值: UserControl1.Designer.cs: // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(64,63); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(35,13); this.label1.TabIndex = 0; // // UserControl1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F,13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.label1); this.Name = "UserControl1"; this.Size = new System.Drawing.Size(181,136); this.ResumeLayout(false); this.PerformLayout(); Form1.Designer.cx: // // userControl11 // this.userControl11.Location = new System.Drawing.Point(35,43); this.userControl11.Name = "userControl11"; this.userControl11.Size = new System.Drawing.Size(181,136); this.userControl11.TabIndex = 0; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F,13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(284,262); this.Controls.Add(this.userControl11); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); 要重现问题,只需创建一个新的Windows窗体应用程序,创建一个带有标签的用户控件,并使用名为“Text”的属性. 解决方法
尝试对Text属性使用override而不是new,并包含DesignerSerializationVisibility属性:
[Browsable(true)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public override string Text { get { return label1.Text; } set { label1.Text = value; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- vb.net – 如何编辑存储在SQL Server表中的Word文档(.docx)
- C#正则匹配RegexOptions选项的组合使用方法
- 揭秘React形成合成事件的过程
- ruby-on-rails – Dotenv多行变量
- c# – 您使用Visual Studio 2005/2008的哪些(第三方)调试可
- c# – 使用EF4(edmx模型)时偶尔“底层提供程序无法打开”错
- c# – 如何解决问题:System.TypeLoadException?
- c – 类中的易失性变量:“在’volatile’之前预期的nonqua
- ios – iTunes Connect:导入此构建时出错
- c – 记录库的意义和目的是什么?