设计者未保存基类的属性值 [英] Property value of base class not saved by designer
问题描述
我已在Visual Studio Express 2013中的WinForms项目中添加了一个表单,希望用作其他表单的基本表单。假设我在此表单上放置了一个按钮,并且我想拥有一个使该按钮可见或不可见的属性。
I have added a form to my WinForms project in Visual Studio Express 2013 that I want to use as a base form for other forms. Say I put a button on this form and I want to have a property that makes this button visible or invisible.
Imports System.ComponentModel
Public Class MyForm
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
Public Property ButtonVisible As Boolean
Get
Return Button1.Visible
End Get
Set(value As Boolean)
Button1.Visible = value
End Set
End Property
End Class
此文件的设计者是没有改变。我只是在新表单中添加了一个按钮。
The designer for this file was not changed. I just added a button to a new form.
当我现在创建一个继承该类的新表单时,可以更改此属性的值,并在设计时更改按钮确实变得可见或不可见。但是,当我编译项目时,该属性会重置为默认值。当我检查派生窗体的设计器文件时,我发现未将更改后的属性值添加到该文件中,因此消失了。
当我将 ButtonVisible = False
手动添加到设计器文件时,它可以正常工作并停留在该文件中,所以我想问题出在以下事实:设计器未添加
When I now create a new form that inherits this class I can change the value of this property and at design time the button indeed becomes visible or invisible. However when I compile the project the property is resetted back to the default value. When I examine the designer file of the derived form I see, that the changed property value is not added to it and therefore vanishes into thin air.
When I add ButtonVisible = False
manually to the designer file it works and stays there, so I guess the problem lies in the fact, that the Designer does not add the line to the designer file.
这是在我更改设计器中的属性值之后,派生表单的设计器文件:
This is the designer file of the derived form, after I changed the property value in the designer:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form2
Inherits MyForm
'Das Formular überschreibt den Löschvorgang, um die Komponentenliste zu bereinigen.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Wird vom Windows Form-Designer benötigt.
Private components As System.ComponentModel.IContainer
'Hinweis: Die folgende Prozedur ist für den Windows Form-Designer erforderlich.
'Das Bearbeiten ist mit dem Windows Form-Designer möglich.
'Das Bearbeiten mit dem Code-Editor ist nicht möglich.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.SuspendLayout()
'
'Form2
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 261)
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)
End Sub
End Class
如您在顶部代码中所见,我已经尝试通过测试 DesignerSerializationVisible的不同值来解决此问题。
,但它们似乎没有作用。
As you can see in the code at the top, I already tried to solve the problem by testing the different values for DesignerSerializationVisible
, but they seem to have no effect.
有什么我忽略的东西吗?
无论您最喜欢哪种方式,C#或VB.NET答案都很受赞赏。
Is there anything I overlooked? How should I add properties that change controls in the underlying base class?
Both C# or VB.NET answers are very much appreciated, whatever you like best.
推荐答案
首先,您似乎误解了 DesignerSerializationVisibility.Content
值 DesignerSerializationVisibility
属性。
First, it seems you misunderstood the DesignerSerializationVisibility.Content
value of DesignerSerializationVisibility
attribute.
您需要使用 DesignerSerializationVisibility.Visible
值保存广告的值属性。
参见以下相关说明:属性不会获得序列化为.designer.cs文件
You need to use the DesignerSerializationVisibility.Visible
value to save the value of a property.
See this related tread: Properties won't get serialized into the .designer.cs file
然后,您不能直接引用按钮.Visible
您的自定义属性中的属性。每次您打开继承的表单
时,按钮的可见状态将重置为其默认值( True
)。因此,在加载表单时,您的自定义属性将始终显示 True
。
Then you can't refer directly to the Button.Visible
property in your custom property. The visibility state of the button will be reset to its default value (True
) each time you open the inherited form
. So your custom property will always show True
when the form will be loaded.
您需要
- 将状态存储在变量中
- 在
InitializeComponent $之后调整按钮的可见性c $ c>方法以及您的财产的价值何时更改。
- store the state in a variable
- adjust the button visibility after
InitializeComponent
method and when the value of your property changes.
Public Class MyForm
Public Sub New()
InitializeComponent()
Me.Button1.Visible = _buttonVisibility
End Sub
Private _buttonVisibility As Boolean = True
<DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)>
Public Property ButtonVisible As Boolean
Get
Return _buttonVisibility
End Get
Set(value As Boolean)
_buttonVisibility = value
Button1.Visible = value
End Set
End Property
End Class
这篇关于设计者未保存基类的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!