的Windows Phone 8 - 枚举&安培; DataAnnotations [英] Windows Phone 8 - enums & DataAnnotations

查看:142
本文介绍了的Windows Phone 8 - 枚举&安培; DataAnnotations的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

被这难道不是一个窗口,电话8的项目,如果我想要显示枚举的人类可读的版本,我会用一个niftt DataAnnotation像这样

 公共枚举InstallationType 
{
[显示(说明=散装头的前方)]
FORWARD = 0,

[显示(说明=散装头的背面)]
后= 1,

[显示(说明=屋顶安装)]
屋顶= 2,
}

和使用的东西的效果将它们拉出来:

 公共静态字符串GetDescriptionFromEnumValue(枚举值)
{
DisplayAttribute属性= value.GetType()
.GetField(value.ToString( ))
.GetCustomAttributes(typeof运算(DisplayAttribute),FALSE)
.SingleOrDefault()作为DisplayAttribute;
返回属性== NULL? value.ToString():attribute.Description;
}



简单,
,而我的问题是,System.ComponentModel.DataAnnotation是不是在Windows Phone的使用。我一直在使用便携式类库尝试,但我的目标WP8我松打该命名空间的能力。



什么其他的选择有哪些?


解决方案

System.ComponentModel.DataAnnotation是不是在Windows Phone的使用,你可以使用LINQ的。
按照这个例子绑定枚举,



在WP8

绑定在WP8

Were this not a window phone 8 project, If I wanted to display a human readable version of an enum I would use a niftt DataAnnotation like such

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,
}

and pull them out using something to the effect of:

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;
    }

Easy, But my problem is that System.ComponentModel.DataAnnotation is not available in Windows Phone. I have tried using a Portable Class Library but when I target WP8 I loose the ability to hit that namespace.

What other options are there?

解决方案

System.ComponentModel.DataAnnotation is not available in Windows Phone , you can make use of Linq. Follow this example to bind enum,

Missing in wp8

Binding enum in wp8

这篇关于的Windows Phone 8 - 枚举&安培; DataAnnotations的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆