我的模型中有以下内容:
public class Equipment
{
public enum Type
{
Detector,
VegetationClearance,
Removal,
Engaging
}
}
Run Code Online (Sandbox Code Playgroud)
并在视图模型中:
private Equipment.Type _equipmentType;
public Equipment.Type EquipmentType
{
get { return _equipmentType; }
set
{
_equipmentType = value;
RaisePropertyChanged(() => EquipmentType);
}
}
Run Code Online (Sandbox Code Playgroud)
我想将这些值用作ItemsSource,以便用户可以从枚举中进行选择:
<Mvx.MvxSpinner
android:id="@+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
local:MvxBind="ItemsSource Equipment.Type; SelectedItem TypeSelection" />
Run Code Online (Sandbox Code Playgroud)
这根本不起作用.有没有办法将枚举绑定为ItemsSource?
编辑:更好的解决方案
正如安德斯评论的那样,Enum.GetValues()可能是一个更好的主意.绑定到枚举的一个问题是标识符不能包含空格,因此默认情况下,绑定不会为您提供良好的可读字符串.
但是,您可以使用Display属性修饰枚举.参考System.ComponentModel.DataAnnotations.
public class Equipment
{
public enum Type
{
Detector,
[Display(Name="Vegetation Clearance")]
VegetationClearance,
Removal,
Engaging
}
}
Run Code Online (Sandbox Code Playgroud)
现在将以下属性添加到ViewModel:
public IEnumerable<Equipment.Type> EquipmentTypes
{
get { return Enum.GetValues(typeof(Equipment.Type)).Cast<Equipment.Type>(); }
}
private Equipment.Type _selectedType;
public Equipment.Type SelectedType
{
get { return _selectedType; }
set { _selectedType = value; RaisePropertyChanged(() => SelectedType); }
}
Run Code Online (Sandbox Code Playgroud)
我们要做的是创建一个Value转换器,它将枚举转换为一个字符串以供显示,如果存在,将返回Display Name属性.
public class EnumDisplayNameValueConverter : MvxValueConverter
{
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return GetEnumDisplayName((Enum)value);
}
public static string GetEnumDisplayName(Enum value)
{
var t = value.GetType();
var ti = t.GetTypeInfo();
var fi = ti.DeclaredFields.FirstOrDefault(x => x.Name == value.ToString());
var attributes = (DisplayAttribute[])fi.GetCustomAttributes(typeof(DisplayAttribute), false);
if (attributes != null && attributes.Length > 0)
{
return attributes[0].Name;
}
return value.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
要使用值转换器,您需要在微调器中指定项模板和下拉模板:
<Mvx.MvxSpinner
android:id="@+id/type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
local:MvxItemTemplate="@layout/spinneritem"
local:MvxDropDownItemTemplate="@layout/spinnerdropdownitem"
local:MvxBind="ItemsSource EquipmentTypes; SelectedItem SelectedType" />
Run Code Online (Sandbox Code Playgroud)
并创建spinneritem/spinnerdropdownitem布局:
<?xml version="1.0" encoding="utf-8" ?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
local:MvxBind="Text EnumDisplayName(.)" />
Run Code Online (Sandbox Code Playgroud)
请注意我们绑定到EnumDisplayName(.).这是值转换器,.意味着当前值是枚举.
我在GitHub上添加了一个示例.https://github.com/kiliman/MvxSpinnerEnumSample
| 归档时间: |
|
| 查看次数: |
1595 次 |
| 最近记录: |