问题
I'm trying to write my own abstraction over the MessageBoxImage enumeration, and see that MessageBoxImage is defined as:
namespace System.Windows
{
public enum MessageBoxImage
{
None = 0,
Error = 16,
Hand = 16,
Stop = 16,
Question = 32,
Exclamation = 48,
Warning = 48,
Asterisk = 64,
Information = 64,
}
}
How does the Show method determine whether to display an Error image or a Hand image? How would I write a method which takes a MessageBoxImage type, and return a CustomMessageBoxImage type which maps to the MessageBoxImage type, as I can't include both MessageBoxImage.Error and MessageBoxImage.Hand in the same switch statement?
回答1:
Historically there were different icons that ended up being merged into a single actual icon image. So there are several enumerated type values (Hand and Stop for example) that simply mean the same thing in modern Windows OSes. There is no difference between them, they are simply aliases.
If you want to have new values to represent differences, then you could use a secondary variable (e.g. "isError) to convey the difference you wish to apply between Stop and Hand. Or you could copy the Icon value into an int and set a high bit in the value to indicate this extra information so it can be "carried" within a single variable. Or you could use your own enumeration that is "unrelated" to the MessageBoxIcon, and have methods that convert from your value to the MessageBoxIcon value.
I'd suggest having your own "Status" value and then converting that into an Icon value as needed - the two are conveying quite different information, so trying to overload (corrupt) the MessageBox value to convey extra information wouldn't be a very good approach.
回答2:
Not all of the enumerations (Error, Information, Stop and Warning) are available in the Compact Framework.
If you are sharing code files between a full Windows client and Compact Framework client then you will need to use Asterisk, Exclamation, Hand, None, or Question enumerations.
https://msdn.microsoft.com/en-us/library/system.windows.forms.messageboxicon(v=vs.80).aspx
回答3:
If one wants, a workaround is to cast the values to int as follows:
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;
}
来源:https://stackoverflow.com/questions/2847313/why-does-system-windows-messageboximage-have-enumeration-sub-items-with-the-same