首页 > 解决方案 > 为什么 Windows 窗体设计器属性中的 BackColor 和 ForeColor Int32s,而设计器代码中的 Drawing.Colors?

问题描述

我期待能够做类似的事情:

public static class CustomColors
{
    Color CustomColor1 { get; set; } = Color.Black;
}

// In the designer:
form1.BackColor = CustomColors.CustomColor1;

这是在 C#、Windows 窗体应用程序中。

但是,我注意到在 Visual Studio 中使用设计器时,BackColor 和 ForeColor 字段将只接受整数并有一个颜色选择器供您选择。如果我想使用我的 CustomColors 类并输入类似的内容CustomColors.CustomColor1,或者System.Drawing.Color.Black

在此处输入图像描述

我收到以下错误:

CustomColors.CustomColor1 不是 Int32 的有效值。

但是,在设计器属性窗口中选择WebthenBlack会在设计器中生成以下代码行:

this.BackColor = System.Drawing.Color.Black;

我可以像这样在设计器代码中输入我的静态颜色:

this.BackColor = CustomColors.CustomColor1;

但随后设计器不起作用(与上述相同的错误)。

定义静态整数也不起作用 - 我试过这个:

public static class CustomColors
{
   Int32 CustomColor1 { get; set; } = Color.Black.ToArgb();
}

这不仅仅是 Windows 窗体控件 - 我尝试使用可浏览的 Color 属性制作用户控件,而且我也仅限于设计器中的颜色选择器。

谁能解释一下?设计师来这里做什么?它是如何在整数和颜色之间转换的,为什么我不能使用我自己的?

标签: c#visual-studiowinformsvisual-studio-2015visual-studio-2017

解决方案


推荐阅读