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

本文关键字:设置 属性 编译 | 更新日期: 2023-09-27 17:51:19

我正在用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

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

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

@汉斯·帕桑特,

谢谢!这确实是错误。我通过添加'= 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;

backbackfields也被移除