mvvmcross:如何将枚举值用作ItemsSource [英] mvvmcross: How to use enum values as ItemsSource

查看:69
本文介绍了mvvmcross:如何将枚举值用作ItemsSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型中包含以下内容:

I have the following in my model:

public class Equipment
{
    public enum Type
    {
        Detector,
        VegetationClearance,
        Removal,
        Engaging
    }
}

在视图模型中:

    private Equipment.Type _equipmentType;
    public Equipment.Type EquipmentType
    {
        get { return _equipmentType; }
        set
        {
            _equipmentType = value;
            RaisePropertyChanged(() => EquipmentType);
        }
    }

我想将这些值用作ItemsSource,以便用户可以从枚举中进行选择:

And I want to use the values as an ItemsSource so that the user can select from the enumeration:

    <Mvx.MvxSpinner
        android:id="@+id/type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        local:MvxBind="ItemsSource Equipment.Type; SelectedItem TypeSelection" />

这根本不起作用.有没有办法将枚举绑定为ItemsSource?

This doesn't work at all. Is there a way to bind an enumeration as an ItemsSource?

推荐答案

更好的解决方案

正如安德斯(Anders)所说,Enum.GetValues()可能是一个更好的主意.绑定到枚举的问题之一是标识符不能包含空格,因此默认情况下,绑定不会为您提供一个易于阅读的字符串.

As Anders commented, Enum.GetValues() is probably a better idea. One of the issues in binding to enums is that the identifier can't include spaces, so by default, binding will not give you a nice readable string.

但是,您可以使用Display属性装饰枚举.参考 System.ComponentModel.DataAnnotations .

However, you can decorate your enum with a Display attribute. Reference System.ComponentModel.DataAnnotations.

public class Equipment
{
    public enum Type
    {
        Detector,
        [Display(Name="Vegetation Clearance")]
        VegetationClearance,
        Removal,
        Engaging
    }
}

现在将以下属性添加到您的ViewModel:

Now add the following properties to your 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); }
}

我们要做的是创建一个 Value Converter ,它将 enum 转换为显示字符串,该字符串将返回 Display Name 属性(如果存在).

What we are going to do is create a Value Converter that converts an enum into a string for display that will return the Display Name attribute if present.

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

要使用值转换器,您需要指定项目模板并在微调器中下拉模板:

In order to use the value converter, you'll need to specify the item template and drop down template in your spinner:

<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" />

并创建spinneritem/spinnerdropdownitem布局:

And create the spinneritem/spinnerdropdownitem layouts:

<?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(.)" />

请注意,我们已绑定到EnumDisplayName(.).那是值转换器,而.表示当前值,它是枚举.

Notice that we bind to EnumDisplayName(.). That is the value converter and the . means the current value which is the enum.

我在GitHub上添加了一个示例. https://github.com/kiliman/MvxSpinnerEnumSample

I've added a sample on GitHub. https://github.com/kiliman/MvxSpinnerEnumSample

这篇关于mvvmcross:如何将枚举值用作ItemsSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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