【问题标题】:Nested properties wont "Stay" set嵌套属性不会“保持”设置
【发布时间】:2016-04-23 00:49:27
【问题描述】:

我正在尝试制作自定义控件库。起初我以为我已经完成了表格并且完全可以工作,但我没有。我不会问如何解决我的表单问题,而是用我的按钮问它。

我正在制作一个自定义按钮,该按钮可针对每种状态进行广泛自定义,因为它能够设置每种颜色以及文本。

我还实现了一种使按钮具有不同方向的方法,这是目前解决我的问题的最简单方法,因为我确信一旦解决了这个问题,我就可以解决我的其余问题。

我会先给你看一些代码然后解释一下。

namespace OurCSharp.OurControls.Core.Buttons.Button
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Linq;
    using System.Windows.Forms;

    using OurCSharp.OurControls.Core.Buttons.Button.Properties;
    using OurCSharp.OurControls.Core.Buttons.Enums;
    using OurCSharp.OurControls.Core.Buttons.Interfaces;

    [DefaultEvent("Click")]
    public class OurButton : Control////, IOurButtonRoot
    {
        private Color _borderColor = Color.FromArgb(255, 25, 25, 25);
        private Color _textColor = Color.FromArgb(255, 150, 150, 150);

        // TODO We will see if this is actually ignored or not; it may be though because it is its default value.
        private SizeF _textSizeF = SizeF.Empty;

        private IOurButtonDesigner _buttonDesigner;



        /// <summary>
        /// The designers for each state of the button.
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public OurButtonDesigner ButtonDesign { get; set; }

        /// <summary>
        /// Gets or sets the background color for the control.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Drawing.Color"/> that represents the background color of the control. The default is the value of the <see cref="P:System.Windows.Forms.Control.DefaultBackColor"/> property.
        /// </returns>
        /// <PermissionSet><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
        [DefaultValue(typeof (Color), "255, 75, 75, 75")]
        public override Color BackColor
        {
            get { return base.BackColor; }
            set
            {
                base.BackColor = value;
                this.Invalidate();
            }
        }

        /// <summary>
        /// Gets or sets the foreground color of the control.
        /// </summary>
        /// <returns>
        /// The foreground <see cref="T:System.Drawing.Color"/> of the control. The default is the value of the <see cref="P:System.Windows.Forms.Control.DefaultForeColor"/> property.
        /// </returns>
        /// <PermissionSet><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
        [DefaultValue(typeof(Color), "255, 150, 150, 150")]
        public override Color ForeColor
        {
            get { return base.ForeColor; }
            set
            {
                base.ForeColor = value;
                this.Invalidate();
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this control should redraw its surface using a secondary buffer to reduce or prevent flicker.
        /// </summary>
        /// <returns>
        /// true if the surface of the control should be drawn using double buffering; otherwise, false.
        /// </returns>
        [DefaultValue(true)]
        protected override bool DoubleBuffered { get { return base.DoubleBuffered; } set { } }

        public OurButton()
        {
            // TODO Might have to add in property for this with a description
            // TODO Gotta update the minimum size...
            base.AutoSize = true;

            base.DoubleBuffered = true;

            this.Padding = new Padding(6, 2, 6, 2);

            base.Text = @"OurButton";

            this.ButtonDesign = new OurButtonDesigner(this);

            Debug.WriteLine(this.ButtonDesign.Clicked.Text);



            Debug.WriteLine(3);
            this.Margin = new Padding(3);

            // TODO Need to re-calcualte size aswell with padding is changed.
            // TODO Also need to re-calculate on when the font is changed.




            base.TextChanged += (sender, args) => this.Invalidate();





            base.Text = @"OurButton";

            // TODO May not need this hear, might just be able to call it in the 'OnCreateControl'
            this.Invalidate();
        }

        public void UpdateSize() { this.Size = this.CalculateSize(); }

        private SizeF MeassureString(string str) => this.CreateGraphics().MeasureString(str, this.Font);

        /// <summary>
        /// Raises the <see cref="M:System.Windows.Forms.Control.CreateControl"/> method.
        /// </summary>
        protected override void OnCreateControl()
        {
            Debug.WriteLine(1);
            base.OnCreateControl();
            Debug.WriteLine(2);

            this._buttonDesigner = this.Enabled ? this.ButtonDesign.Normal : this.ButtonDesign.Disabled;

            base.BackColor = this._buttonDesigner.BackColor;
            base.ForeColor = this._buttonDesigner.TextColor;

            var stringToArea = new KeyValuePair<string, float>(string.Empty, 0F);

            foreach (var str in new[]
                                {
                                    this.ButtonDesign.Normal.Text,
                                    this.ButtonDesign.Hovered.Text,
                                    this.ButtonDesign.Clicked.Text,
                                    this.ButtonDesign.Disabled.Text
                                })
            {
                var area = 0F;

                Debug.WriteLine(str);



                // TODO This may not work.
                if (stringToArea.Value >= (area = this.CalculateStringArea(str))) { continue; }

                stringToArea = new KeyValuePair<string, float>(str, area);
            }

            this._textSizeF = this.MeassureString(stringToArea.Key);

            this.Size = this.CalculateSize();
            this.Text = this._buttonDesigner.Text;

            Debug.WriteLine("HERE");

            Debug.WriteLine("{0}, {1}, {2}", this.Text, stringToArea.Key, stringToArea.Value);

            this.Invalidate();
        }

        // Calculating the Area is beter than Parimeter because Parimeter could be used for something like a Polygon.
        private float CalculateStringArea(string str)
        {
            var sizeF = this.MeassureString(str);
            return sizeF.Width * sizeF.Height;
        }

        ////private Size CalculateSize()
        ////    =>
        ////        (this._textSizeF =
        ////         new SizeF(this.ButtonDesign.Orientation == ButtonOrientation.Verticle
        ////                       ? new Size((int)this._textSizeF.Height + this.Padding.Bottom + this.Padding.Top,
        ////                                  (int)this._textSizeF.Width + this.Padding.Left + this.Padding.Right)
        ////                       : new Size((int)this._textSizeF.Width + this.Padding.Left + this.Padding.Right,
        ////                                  (int)this._textSizeF.Height + this.Padding.Bottom + this.Padding.Top))).ToSize
        ////            ();

        private Size CalculateSize()
        =>
            (
             new SizeF(this.ButtonDesign.Orientation == ButtonOrientation.Verticle
                           ? new Size((int)this._textSizeF.Height + this.Padding.Bottom + this.Padding.Top,
                                      (int)this._textSizeF.Width + this.Padding.Left + this.Padding.Right)
                           : new Size((int)this._textSizeF.Width + this.Padding.Left + this.Padding.Right,
                                      (int)this._textSizeF.Height + this.Padding.Bottom + this.Padding.Top))).ToSize
                ();

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Control.MouseClick"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.Windows.Forms.MouseEventArgs"/> that contains the event data. </param>
        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);

            // TODO Using a trenary may use more resources as it's always going to update the UI even if it's already disabled..
            /*
             * Need re-assign text before the 'BackColor' and 'ForeColor' because once those are called,
             * the button is going to set invalidated.
             */
            this.Text =
                (this._buttonDesigner = this.Enabled ? this.ButtonDesign.Clicked : this.ButtonDesign.Disabled).Text;
            this.BackColor = this._buttonDesigner.BackColor;
            this.ForeColor = this._buttonDesigner.TextColor;

            // TODO May have to set the state back to something different after being clicked
        }

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Control.MouseEnter"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data. </param>
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);

            // TODO Using a trenary may use more resources as it's always going to update the UI even if it's already disabled..
            /*
             * Need re-assign text before the 'BackColor' and 'ForeColor' because once those are called,
             * the button is going to set invalidated.
             */
            this.Text =
                (this._buttonDesigner = this.Enabled ? this.ButtonDesign.Hovered : this.ButtonDesign.Disabled).Text;
            this.BackColor = this._buttonDesigner.BackColor;
            this.ForeColor = this._buttonDesigner.TextColor;
        }

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Control.MouseLeave"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data. </param>
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);

            // TODO Using a trenary may use more resources as it's always going to update the UI even if it's already disabled..
            /*
             * Need re-assign text before the 'BackColor' and 'ForeColor' because once those are called,
             * the button is going to set invalidated.
             */
            this.Text =
                (this._buttonDesigner = this.Enabled ? this.ButtonDesign.Normal : this.ButtonDesign.Disabled).Text;
            this.BackColor = this._buttonDesigner.BackColor;
            this.ForeColor = this._buttonDesigner.TextColor;
        }

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Control.Paint"/> event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs"/> that contains the event data. </param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var g = e.Graphics;
            var p = new Pen(this._borderColor);

            var clientRect = this.ClientRectangle;

            clientRect.Width--;
            clientRect.Height--;

            if (this._buttonDesigner.PaintBorder)
            {
                p.Color = this._buttonDesigner.BorderColor;
                g.DrawRectangle(p, clientRect);
            }

            var b = new SolidBrush(this.ForeColor);

            switch (this.ButtonDesign.Orientation)
            {
                case ButtonOrientation.Horizontal:
                    g.DrawString(this.Text,
                                 this.Font,
                                 b,
                                 (int)this._textSizeF.Width / 2F + this.Padding.Left,
                                 this.Padding.Top,
                                 new StringFormat()
                                 {
                                     Alignment = StringAlignment.Center
                                 });
                    break;
                case ButtonOrientation.Verticle:
                    g.DrawString(this.Text,
                                 this.Font,
                                 b,
                                 this.Padding.Top,
                                 (int)this._textSizeF.Width / 2F + this.Padding.Left,
                                 new StringFormat()
                                 {

                                     FormatFlags = StringFormatFlags.DirectionVertical,
                                     LineAlignment = StringAlignment.Center
                                 });
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            ////g.DrawString(this.Text,
            ////             this.Font,
            ////             b,
            ////             3,
            ////             this.Padding.Top, ////this._textSizeF.Height / 2F + 3F,
            ////             this.ButtonDesign.Orientation == ButtonOrientation.Verticle
            ////                 ? new StringFormat
            ////                   {
            ////                       Alignment = StringAlignment.Center,
            ////                       FormatFlags = StringFormatFlags.DirectionVertical
            ////                   }
            ////                 : new StringFormat
            ////                   {
            ////                       Alignment = StringAlignment.Center
            ////                   });
        }
    }
}

这里是ButtonBaseClass

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OurCSharp.OurControls.Core.Buttons.Button.Properties
{
    using System.ComponentModel;

    using OurCSharp.OurControls.Core.Buttons.Button.Properties.SubProperties;
    using OurCSharp.OurControls.Core.Buttons.Enums;
    using OurCSharp.OurControls.Core.Buttons.Interfaces;

    public class OurButtonDesigner : IOurButtonBase
    {
        private readonly OurButton _ourButton;

        private ButtonOrientation _orientation = ButtonOrientation.Horizontal;

        public OurButtonDesigner(OurButton ourButton)
        {
            this._ourButton = ourButton;
            this.Normal = new OurButtonHovered(ourButton);
            this.Hovered = new OurButtonHovered(ourButton);
            this.Clicked = new OurButtonClicked(ourButton);
            this.Disabled = new OurButtonDisabled(ourButton);
        }

        /// <summary>
        /// The orientation of the button.
        /// </summary>
        [DefaultValueAttribute(typeof(ButtonOrientation), "Horizontal")]
        public ButtonOrientation Orientation
        {
            get { return this._orientation; }
            set
            {
                this._orientation = value;
                this._ourButton.UpdateSize();
                this._ourButton.Invalidate();
            }
        }

        /// <summary>
        /// Designer for the button when in the 'Normal' state.
        /// </summary>
        public IOurButtonDesigner Normal { get; }

        /// <summary>
        /// Designer for the button when in the 'Hovered' state.
        /// </summary>
        public IOurButtonDesigner Hovered { get; }

        /// <summary>
        /// Designer for the button when in the 'Clicked' state.
        /// </summary>
        public IOurButtonDesigner Clicked { get; }

        /// <summary>
        /// Designer for the button when in the 'Designer' state.
        /// </summary>
        public IOurButtonDesigner Disabled { get; }
    }
}

我很抱歉它看起来很垃圾,我通常没有这样的代码。它通常非常整洁。

回到主题。

让我们只关注方向。

我也知道我的代码中有很多不应该存在的杂物,但这正是我尝试过但失败的地方。

方向属性在设计时显示在属性网格中,如果我更改它,它会将表单上的按钮更新为垂直或水平。默认是水平的。

现在,当我将其更改为垂直时,它会像之前所说的那样发生变化,但是一旦我运行代码对其进行测试,它就会回到默认的水平。

我认为问题是我在代码运行时实例化了每个类,但我无法找到修复它的方法。

我真的需要帮助,有人可以帮帮我吗?

【问题讨论】:

    标签: c# debugging properties nested designer


    【解决方案1】:

    你需要告诉设计者设置嵌套对象的属性:

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public OurButtonDesigner ButtonDesign { get; set; }
    

    【讨论】:

    • 假设我有一个接口,它的成员属于另一个接口,如果我将属性放在这些成员上(请记住它们是一个接口),例如你向我展示的那个,如果我实现该接口,实现接口中的接口成员上的属性(如果没有以斜体显示,我很抱歉,我仍在学习如何使用此表单)用过的?另外,假设我有一个嵌套属性,它也是一个嵌套属性。我是否还必须装饰每个嵌套类?还是只是最顶级的?
    • 对于接口与运行时类型,我不确定;实验。 (查看.Designer.cs,或尝试将[Description] 应用于两者,看看使用的是哪个;所有属性都来自同一个成员)
    • 您需要将其应用于您要序列化其内容的每个属性。
    • 那么对于我展示的代码的上下文,我只需要把它放在OurButtonDesigner上?还是“OurButtonDesigner”属性中的属性?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多