.net 为什么System.Windows.MessageBoxImage具有相同值的枚举子项?
我试图在MessageBoxImage枚举上编写自己的抽象,并将MessageBoxImage定义为:.net 为什么System.Windows.MessageBoxImage具有相同值的枚举子项?,.net,windows,messagebox,.net,Windows,Messagebox,我试图在MessageBoxImage枚举上编写自己的抽象,并将MessageBoxImage定义为: namespace System.Windows { public enum MessageBoxImage { None = 0, Error = 16, Hand = 16, Stop = 16, Question = 32, Exclamat
namespace System.Windows
{
public enum MessageBoxImage
{
None = 0,
Error = 16,
Hand = 16,
Stop = 16,
Question = 32,
Exclamation = 48,
Warning = 48,
Asterisk = 64,
Information = 64,
}
}
Show方法如何确定是显示错误图像还是显示手部图像?
如何编写一个方法,该方法采用MessageBoxImage类型,并返回一个映射到MessageBoxImage类型的CustomMessageBoxImage类型,因为我不能同时包含MessageBoxImage.Error和MessageBoxImage.Hand这两个switch语句?历史上,不同的图标最终合并为一个实际的图标图像。因此,在现代Windows操作系统中,有几个枚举类型值(例如Hand和Stop)的含义完全相同。它们之间没有区别,它们只是别名 如果您希望有新的值来表示差异,那么您可以使用辅助变量(例如“isError”)来传达您希望在Stop和Hand之间应用的差异。或者您可以将图标值复制到int中,并在值中设置一个高位以指示此额外信息,以便可以“携带”“在单个变量中。或者,您可以使用与MessageBoxIcon“无关”的枚举,并使用从您的值转换为MessageBoxIcon值的方法
我建议您使用自己的“状态”值,然后根据需要将其转换为图标值-这两个值传递的信息完全不同,因此尝试重载(损坏)MessageBox值来传递额外信息不是一个很好的方法。并非所有枚举(错误、信息、停止和警告)在紧凑的框架中提供 如果要在完整Windows客户端和Compact Framework客户端之间共享代码文件,则需要使用星号、感叹号、手动、无或问题枚举
如果需要,解决方法是将值强制转换为int,如下所示:
var icon = MessageBoxImage.Error;
switch ((int)icon)
{
case (int)MessageBoxImage.Error:
// Reached by setting icon above to "Hand" and "Stop" as well.
break;
case (int)MessageBoxImage.Question:
break;
case (int)MessageBoxImage.Warning:
// Reached by setting icon above to "Exclamation" as well.
break;
case (int)MessageBoxImage.Information:
// Reached by setting icon above to "Asterisk" as well.
break;
default:
case (int)MessageBoxImage.None:
break;
}
您不需要在同一个switch语句中包含它们,因为它们都具有相同的值。这意味着就计算机而言,它们是等价的。