我有一个复杂的WPF应用程序,在该应用程序中,我在第三方UI库使用的MessageBoxIcon枚举和System.Windows.MessageBoxImage
枚举之间进行转换。然而,应该是一个非常简单的转换方法(只是一个switch语句)实际上是返回废话。我无法理解为什么会这样,但我把它简化为一个非常简单的演示。
这是一个非常简单的WPF应用程序。
namespace WpfApplication1
{
using System.Diagnostics;
using System.Windows;
public static class Test
{
public static MessageBoxImage GetImage()
{
MessageBoxImage image = MessageBoxImage.Error;
Trace.WriteLine("Test.GetImage, image: " + image);
return image;
}
}
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MessageBoxImage image = Test.GetImage();
Trace.WriteLine("OnStartup, image: " + image);
}
}
}
输出两条跟踪消息:
Test.GetEnum, image: Hand
OnStartup, image: Hand
但为什么呢?为什么他们不输出错误?
答案 0 :(得分:0)
MessageBoxImage枚举有3个值,Error,Hand和Stop,都具有相同的数值16。这就是为什么你可以得到一个不同于你设置的值的原因。
请看这里:Why does System.Windows.MessageBoxImage have enumeration sub-items with the same value?。