本文介绍了Visual Studio 2010中的错误是否继承了Form属性持久性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从另一个继承了一个WinForms表单。我有一个基本形式的属性:

I am inheriting one WinForms Form from another. I have a property in the base form:

[Browsable(true)]
[Category("Appearance")]
[DefaultValue(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public bool ShowMeLabelVisible
{
    get { return ShowMeLabel.Visible; }
    set { ShowMeLabel.Visible = value; }
}

public bool ShouldSerializeShowMeLabelVisible ()
{
    return true;
}



继承表单中生成的InitializeComponent()方法仅在值为true时设置属性值,并且在设置属性值时不设置属性值false,与DefaultValueAttribute指定的默认值无关。尽管ShouldSerializeShowMeLabelVisible方法指示应始终序列化属性值。此外,当表单首次显示在Visual Studio设计器中时,或者在重建(包括调试运行)后立即显示时,Visual Studio会在属性窗口中将属性值显示为true,并且设计器的行为就像将属性设置为真正;即使DefaultValueAttribute是用false构造的,底层的ShowMeLabel.Visible属性也是false,并且(由于IntializeComponent()中没有初始化),运行时值为false。



有人能给我一个关于我做错的提示吗?我想在设计时和运行时的属性值达成一致,设计师看起来像运行时表。



环境是VS 2010版10.0 Win2 Pro 6.1.7601 SP1上的.40219.1 SP1Rel。



谢谢。


The generated InitializeComponent() method in the inheriting form sets the property value only when it is true, and does not set the property value when it is false, regardless of the default value specified by the DefaultValueAttribute. This is despite the ShouldSerializeShowMeLabelVisible method indicating the property value should always be serialized. Further, when the form is first shown in the Visual Studio designer, or immediately after a rebuild (including a debug run), Visual Studio shows the property value as true in the Properties window, and the designer acts as if the property were set to true; even if the DefaultValueAttribute is constructed with false, the underlying ShowMeLabel.Visible property is false, and (due to the absence of initialization in IntializeComponent()) the run time value is false.

Can someone give me a hint as to what I’m doing wrong? I would like the property value at design time and at run time to agree, and the designer to look like the run time form.

The environment is VS 2010 version 10.0.40219.1 SP1Rel on Win 7 Pro 6.1.7601 SP1.

Thank you.

推荐答案

private bool _pt1Visible = true;

[Browsable(true), Category("Appearance"), DefaultValue(true)]
public bool Pt1Visible
{
    get { return _pt1Visible; }
    set
    {
        _pt1Visible = value;
        PotentialLabel1.Visible = _pt1Visible;
    }
}



谢谢!


Note

Either apply the DefaultValueAttribute or provide ResetPropertyName and ShouldSerializePropertyName methods. Do not use both.





正如谢尔盖所​​说,不要依赖方法名称的这种肮脏技巧;最好先使用属性,看看它的行为方式。



As Sergey stated, do not rely on this dirty trick with method names ; better use the attribute in the first place, and see how it is behaving.


这篇关于Visual Studio 2010中的错误是否继承了Form属性持久性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:50