c# - Windows Phone 8 - 枚举和数据注释

标签 c# windows-phone-8 enums

如果这不是一个 Windows Phone 8 项目,如果我想显示人类可读的枚举版本,我会使用像这样的 niftt DataAnnotation

public enum InstallationType
{
   [Display(Description="Forward of Bulk Head")]
    FORWARD = 0,

   [Display(Description="Rear of Bulk Head")]
    REAR = 1,

   [Display(Description="Roof Mounted")]
    ROOF = 2,
}

并使用某种东西将它们拉出来,以达到以下效果:

public static string GetDescriptionFromEnumValue(Enum value)
    {
        DisplayAttribute attribute = value.GetType()
            .GetField(value.ToString())
            .GetCustomAttributes(typeof(DisplayAttribute), false)
            .SingleOrDefault() as DisplayAttribute;
        return attribute == null ? value.ToString() : attribute.Description;
    }

简单, 但我的问题是 System.ComponentModel.DataAnnotation 在 Windows Phone 中不可用。我尝试过使用可移植类库,但是当我以 WP8 为目标时,我失去了访问该命名空间的能力。

还有哪些其他选择?

最佳答案

System.ComponentModel.DataAnnotation 在 Windows Phone 中不可用,您可以使用 Linq。 按照这个例子绑定(bind)枚举,

Missing in wp8

Binding enum in wp8

关于c# - Windows Phone 8 - 枚举和数据注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22957640/

相关文章:

windows-phone-8 - Windows Phone中的Unity崩溃解析

combobox - 为什么 Resharper 认为这些枚举从未被使用过?

C# 将字符串 [] 数组与通用对象列表进行比较

c# - 如何指定 T 是从其他接口(interface)继承的。通用比较器

c# - datetime.TryParseExact 具有不同的值格式

c++ - 如何在 Windows Phone 运行时组件中使用 native 类

c# - 如何重新启动应用程序?

c# - 为什么拆箱枚举会产生奇怪的结果?

typescript - 如何在 Next.js 中使用 next-env.d.ts 文件?

c# - Entity Framework 和 MySQL 的随机顺序