编译时一些 属性 值被重置。 (在设计器中设置)

When compiling some property values are reset. (Set in the designer)

我正在用 C# 编写程序。对于这个程序,我想将一些自定义控件添加到我的主窗体中。

我制作了自己的名为 'ResourceBar' 的进度条,它覆盖了 ProgressBar。我添加了一些可以在设计器中设置的自定义属性。但是,每当我构建这些值时,这些值都会重置为我不想要的值。

    #region Special properties
    private Brush fillBrush = new SolidBrush(Color.Black);
    [DefaultValue(typeof(Color), "Black")]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Category("Custom"), Description("Color of the resource.")]
    public Color FillColor
    {
        get
        {
            return (fillBrush as SolidBrush).Color;
        }
        set
        {
            this.fillBrush = new SolidBrush(value);
        }
    }

    private Brush fontBrush = new SolidBrush(Color.Black);
    [DefaultValue(typeof(Color), "Black")]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Category("Custom"), Description("Font Color of the text above the resource bar.")]
    public Color FontColor
    {
        get
        {
            return (fontBrush as SolidBrush).Color;
        }
        set
        {
            this.fontBrush = new SolidBrush(value);
        }
    }

    private bool drawIfNonZero;
    [DefaultValue(true)]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Category("Custom"), Description("If set to true, the ResourceBar is only drawn if maximum is higher than zero.")]
    public bool DrawIfNonZero
    {
        get { return drawIfNonZero; }
        set { drawIfNonZero = value; }
    }

    private bool drawMaximum;
    [DefaultValue(true)]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Category("Custom"), Description("If set to true, the ResourceBar will show the max amount on top of the bar.")]
    public bool DrawMaximum
    {
        get { return drawMaximum; }
        set { drawMaximum = value; }
    }
    private bool fillResourceBar;
    [DefaultValue(true)]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Category("Custom"), Description("If set to true, the ResourceBar will fill the bar according to the value and maximum.")]
    public bool FillResourceBar
    {
        get { return fillResourceBar; }
        set { fillResourceBar = value; }
    }
    #endregion

我以前没有支持字段。我添加了它们希望它能解决我的问题,但它并没有解决它。

@Hans Passant,

谢谢!这确实是错误。 我通过添加 '= true'

来修复它
    [DefaultValue(true)]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Category("Custom"), Description("If set to true, the ResourceBar is only drawn if maximum is higher than zero.")]
    public bool DrawIfNonZero
    {
        get; set;
    }

    [DefaultValue(true)]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Category("Custom"), Description("If set to true, the ResourceBar will show the max amount on top of the bar.")]
    public bool DrawMaximum
    {
        get; set;
    } = true;

    [DefaultValue(true)]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Category("Custom"), Description("If set to true, the ResourceBar will fill the bar according to the value and maximum.")]
    public bool FillResourceBar
    {
        get; set;
    } = true;

也删除了支持字段。