有人可以给我一个示例如何将枚举绑定到 Windows Phone 8 上的列表选择器吗?
在 Internet 上找不到任何内容……而且缺少有关此工具包的文档也无济于事。
谢谢
有人可以给我一个示例如何将枚举绑定到 Windows Phone 8 上的列表选择器吗?
在 Internet 上找不到任何内容……而且缺少有关此工具包的文档也无济于事。
谢谢
Binding that is easy. The only problem is that the extension method GetNames()
is not available in windows phone. However, you can write one.
public static class EnumExtensions {
public static IEnumerable<string> GetNames<TEnum>() where TEnum : struct {
var type = typeof(TEnum);
if (!type.IsEnum)
throw new ArgumentException(String.Format("Type '{0}' is not an enum", type.Name));
return (
from field in type.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)
where field.IsLiteral
select field.Name)
.ToList<string>();
}
}
Once you have that it is easy to bind it to any list.
public enum MyEnum {
v1, v2, v3
}
// Binding
myListPicker.ItemsSource = EnumExtensions.GetNames<MyEnum>();
// Getting selected value
var myEnum = (MyEnum)Enum.Parse(typeof(MyEnum), myListPicker.SelectedItem.ToString());