以基本形式定义的自定义属性在重建时会以继承形式失去其状态 [英] Custom properties defined in base form lose their state in inherited form upon rebuild

查看:96
本文介绍了以基本形式定义的自定义属性在重建时会以继承形式失去其状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了基本表单的属性无法在继承表单中维持状态的麻烦。

I am having trouble with properties of a base form not maintaining state in an inherited form.

环境:


  • Visual Studio 2010 Ultimate Service Pack 1:版本10.0.40219.1 SP1Rel

  • .Net Framework:版本4.0.30319 SP1Rel

  • Windows 7 Ultimate

下面是源代码和再现步骤:

Below, is the source code and steps to reproduce:

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Test
{
    public partial class BaseForm : Form
    {
        [DefaultValueAttribute(true)]
        public bool ControlVisible
        {
            get
            {
                return this.checkBox1.Visible;
            }
            set
            {
                this.checkBox1.Visible = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool ControlChecked
        {
            get
            {
                return this.checkBox1.Checked;
            }
            set
            {
                this.checkBox1.Checked = value;
            }
        }

        public BaseForm()
        {
            InitializeComponent();
        }
    }
}

上面的默认属性与[DefaultValueAttribute]匹配,即在InitializeComponent()中将checkBox1.Visible设置为true,而checkBox1.Checked为false。这些是将控件放到窗体上时的默认设置。

In the above the default properties match up with the [DefaultValueAttribute], i.e. in InitializeComponent() checkBox1.Visible is set to true and checkBox1.Checked is false. These are the default settings for the control when dropped onto the form.

然后,我创建了以下继承的窗体:

I then created the following inherited form:

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : BaseForm
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

问题和步骤再现:


  1. 在设计器中打开Form1时,属性处于以下状态。

  1. When I open Form1 in the designer the properties are the in following state.

状态:ControlChecked = False-ControlVisible = (粗体)

State: ControlChecked = False - ControlVisible = True (bold)

ControlVisible 设置为 True ,但是它是粗体。 [DefaultValueAttribute]在基本形式中设置为 true ,因此,我希望此属性不会以粗体显示。

ControlVisible is set to True as expected, however it is bold. The [DefaultValueAttribute] is set to true in the base form, so I would have expected this property to not be in bold.

现在我在设计器中将 ControlVisible 更改为 False 。粗体关闭。

I now change ControlVisible to False in the designer. The bold turns off.

状态:ControlChecked = False-ControlVisible = False

State: ControlChecked = False - ControlVisible = False

我现在重建项目和Form1中的代码将重新生成。 ControlVisible 属性恢复为粗体的 True

I now rebuild the project and the code in Form1 gets regenerated. The ControlVisible property reverts back to True in bold.

状态:ControlChecked = False-ControlVisible = True (粗体)

State: ControlChecked = False - ControlVisible = True (bold)

我现在将 ControlChecked False 更改为 True ,它会按预期变为粗体。

I now change ControlChecked from False to True and it becomes bold as expected.

状态:ControlChecked = True (粗体)-ControlVisible = True (粗体)

State: ControlChecked = True (bold) - ControlVisible = True (bold)

我重建项目,没有任何更改。

I rebuild the project and no change.

状态:ControlChecked = (粗体)-ControlVisible = (粗体)

State: ControlChecked = True (bold) - ControlVisible = True (bold)

我现在更改以下项的属性从 True False ControlVisible ,然后重新生成项目。 ControlVisible 以粗体显示回 True

I now change the property of ControlVisible from True to False and rebuild the project again. ControlVisible flipped back to True in bold.

状态:ControlChecked = True (粗体)-ControlVisible = (粗体)

State: ControlChecked = True (bold) - ControlVisible = True (bold)

ControlChecked 似乎按预期工作。当 ControlVisible 设置为false且粗体反转时,它会继续翻转回 True 。在我看来,基本形式的 true 的[DefaultAttributeValue]无法识别。

ControlChecked appears to be working as expected. ControlVisible keeps flipping back to True when it is set to false and the bold is reversed. Seems to me that somehow the [DefaultAttributeValue] of true in the base form is not recognized.

更新:
进行了修订,以修复错误并更准确地隔离问题。

Update: Revised to fix a bug and more exactly isolate the issue.

更新:
如果设置了checkBox1.Visible =在BaseForm的构造函数中为false,则一切正常。因此,底线似乎是麻烦在于要以继承形式在自定义属性中识别 true 的DefaultValueAttribute。

Update: If I set checkBox1.Visible = false in the constructor of BaseForm, then everything works as expected. So, bottom line it appears that the trouble is getting the DefaultValueAttribute of true to be recognized on the custom property in the inherited form.

推荐答案

您的 checkBox1 是基类的私有对象吗?如果不是,那么应该这样做,因为设计人员将序列化两个设置器(一个用于checkBox1.Visible,另一个用于ControlVisible),并且只有序列化的顺序才能确定最终状态,这很不好。

Is your checkBox1 private in the base class? If it's not then it should be, because the designer will serialize both setters (one for checkBox1.Visible and the other for ControlVisible), and only the order of serialization will determine the final state, which is bad.

还要查看Form1.designer.cs文件中自动生成的InitializeControls方法,它是否将自定义属性显式设置为默认值?

Also, look at the autogenerated InitializeControls method in the Form1.designer.cs file, does it explicitly set the custom properties to default values?

最后,尝试使用 ShouldSerialze和重置看看你是否有不同的行为。

Finally, try using ShouldSerialze and Reset and see if you get different behavior.

编辑

我已经在本地重新创建了问题,并附加了一个VS实例调试另一个。 VS第一次对属性 ControlVisible 进行评估时,它返回 false ,因为 checkBox1.Visible (吸气剂来源)为 false 。创建并显示实际的复选框控件后,滚动属性窗口以显示 ControlVisible 时,将再次评估其值并返回 true 默认值,但是VS内部似乎已经将该属性标记为已修改,因为它的 initial 值与默认值不同。这可能是VS中的错误。

I've recreated the problem locally and attached one VS instance to debug another. The first time the property ControlVisible was evaluated by VS it returned false because checkBox1.Visible (the getter source) was false. When the actual checkbox control was created and shown, and the Properties window was scrolled to display ControlVisible it's value was evaluated again and it returned true which is the default value, however it seems that VS internally already tagged that propery as modified, since it's initial value was different than the default. This may be a bug in VS.

我创建了一个简单的效果演示:

I've created a simple demonstration of the effect:

    public BaseForm()
    {
        InitializeComponent();
        _testValue = false;
    }

    private bool _testValue;

    [DefaultValue(true)]
    public bool TestProperty
    {
        get { return _testValue; }
        set { _testValue = value; }
    }

    protected override void OnVisibleChanged(EventArgs e)
    {            
        _testValue = true;
        base.OnVisibleChanged(e);
    }
}

继承上述 BaseForm时 TestPropery 的行为与示例中的 ControlVisible 完全一样,所以我认为这是一个错误在VS中。

When inheriting the above BaseForm the TestPropery behaves exactly like the ControlVisible in your example, so I think it's a bug in VS.

解决此问题的方法是使用简单的布尔支持字段作为助手,因为checkBox1.Visible是不可靠的:

The solution to your problem is to use a simple bool backing field as a helper, because checkBox1.Visible is unreliable:

    public BaseForm()
    {
        InitializeComponent();
        checkBox1.Visible = _controlVisible = true;
    }

    private bool _controlVisible;

    [DefaultValue(true)]
    public bool ControlVisible
    {
        get { return _controlVisible; }
        set { _controlVisible = checkBox1.Visible = value; }
    }

这篇关于以基本形式定义的自定义属性在重建时会以继承形式失去其状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆