c# - 为什么 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 的有效值。
但是,在设计器属性窗口中选择Web
thenBlack
会在设计器中生成以下代码行:
this.BackColor = System.Drawing.Color.Black;
我可以像这样在设计器代码中输入我的静态颜色:
this.BackColor = CustomColors.CustomColor1;
但随后设计器不起作用(与上述相同的错误)。
定义静态整数也不起作用 - 我试过这个:
public static class CustomColors
{
Int32 CustomColor1 { get; set; } = Color.Black.ToArgb();
}
这不仅仅是 Windows 窗体控件 - 我尝试使用可浏览的 Color 属性制作用户控件,而且我也仅限于设计器中的颜色选择器。
谁能解释一下?设计师来这里做什么?它是如何在整数和颜色之间转换的,为什么我不能使用我自己的?
解决方案
推荐阅读
- c# - 如何删除所有注册表项而不会被一个不可删除的键停止?
- java - 我如何创建一种方法来检查帐户中是否有足够的资金允许提款或资金不足
- python-3.x - 在 matplotlib 中缩小图形?
- pygame - Pygame- Screen.blit(source, dest, area) 返回空矩形
- excel - NumberFormat 属性在需要返回格式化日期的函数中未生效
- php - 使用“source”命令获取提供意外输出的变量
- c# - 为什么在静态方法可以是非静态的时候使用它?
- python - 如何在无需安装 python 的情况下向用户分发 aa python 脚本
- matlab - 如何在 MatLab 中使用 ode45 修复分配错误(ode45 函数的第 488 行)
- android - 在 Android WebView 中访问 HTTP 响应标头