使用valueconverter绑定枚举不会显示文本

时间:2014-05-19 01:25:59

标签: mvvmcross

我正在尝试将枚举绑定到RadioGroup,但文本未显示在标题中。我错过了什么?

我的ViewModel具有以下内容:

    private List<Incident.Agent> _agentList = new List<Incident.Agent>
    {
        Incident.Agent.Uxo, Incident.Agent.Mine, Incident.Agent.Other
    };

    public List<Incident.Agent> AgentList
    {
        get { return _agentList; }
        set
        {
            _agentList = value;
            RaisePropertyChanged(() => AgentList);
        }
    }

    private Incident.Agent _agent;

    public Incident.Agent IncidentAgent
    {
        get { return _agent; }
        set
        {
            _agent = value;
            RaisePropertyChanged(() => IncidentAgent);
        }
    }

我的斧头有:

    <Mvx.MvxRadioGroup
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      local:MvxItemTemplate="@layout/item_radio"
      local:MvxBind="ItemsSource AgentList, Converter=IncidentAgent; SelectedItem IncidentAgent, Converter=IncidentAgent" />

Item_Radio.axml:

    <?xml version="1.0" encoding="utf-8"?>
    <RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:local="http://schemas.android.com/apk/res-auto"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      local:MvxBind="Text" />

值转换器是:

public sealed class IncidentAgentValueConverter : MvxValueConverter<Incident.Agent, string>
{
    protected override string Convert(Incident.Agent value, Type targetType, object parameter, CultureInfo culture)
    {
        switch (value)
        {
            case Incident.Agent.Mine:
                return "Mine";
            case Incident.Agent.Uxo:
                return "UXO";
            case Incident.Agent.Other:
                return "Other";
        }
        throw new InvalidOperationException(string.Format("Can't convert Incident.Agent from {0}", value));
    }

    protected override Incident.Agent ConvertBack(string value, Type targetType, object parameter, CultureInfo culture)
    {
        switch (value)
        {
            case "Mine":
                return Incident.Agent.Mine;
            case "UXO":
                return Incident.Agent.Uxo;
            case "Other":
                return Incident.Agent.Other;
        }
        throw new InvalidOperationException(string.Format("Can't convert Incident.Agent from '{0}'", value));
    }
}

上面我没有看到任何文字或检查按钮。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的代码包含ItemsSource AgentList, Converter=IncidentAgent; - 它会尝试将IncidentAgent转换器应用于列表。

尝试在模板中应用转换器 - 与您引用的示例中完全相同 - https://stackoverflow.com/a/23706333/1630816