使用MVVMCROSS在TextView上使用多值转换器

时间:2014-10-09 10:56:59

标签: android xamarin xamarin.android mvvmcross

我一直在努力在单个TextView上使用两个值转换器

基本上我想格式化文本并根据特定值更改TextColor。

以下是我试图实现这一目标的方法。

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/myCustomId"
        android:textColor="@color/gray_color"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall"
        local:MvxBind="Text DateValue, Converter=FormatDate; TextColor Flag, Converter=StatusToColor" />

以下是我的两个转换器

public class FormatDateValueConverter : MvxValueConverter<DateTime, string>
    {
        protected override string Convert(DateTime value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {            
            //code for formatting date    
            return FormattedDate;
        }
    }

==========================

public class StatusToColorValueConverter : MvxColorValueConverter
    {
        protected override Cirrious.CrossCore.UI.MvxColor Convert(object value, object parameter, System.Globalization.CultureInfo culture)
        {
            Cirrious.CrossCore.UI.MvxColor _Color;
            switch ((int)value)
            {
                case 1:
                    _Color = new Cirrious.CrossCore.UI.MvxColor(93, 210, 85, 1);//green
                    break;
                case 2:
                    _Color = new Cirrious.CrossCore.UI.MvxColor(255, 210, 0, 1);//red
                    break;

                case 3:
                    _Color = new Cirrious.CrossCore.UI.MvxColor(208, 14, 13, 1);//yellow
                    break;
                default:
                    _Color = new Cirrious.CrossCore.UI.MvxColor(208, 14, 13, 150);//some color
                    break;
            }
            return _Color;
        }
    }

两个转换器都被调用,但我在文本视图中没有得到任何值。

任何人都可以指导解决这个问题。

由于 Aaman

============================ 以下是相同

的堆栈跟踪
[0:] 
MvxBind:Warning: 17.34 Value '' could not be parsed as a valid string identifier
[0:] MvxBind:Warning: 17.34 Value '' could not be parsed as a valid string identifier
10-09 19:04:08.972 I/mono-stdout(19422): MvxBind:Warning: 17.34 Value '' could not be parsed as a valid string identifier
[0:] 
MvxBind:Warning: 17.36 Value '' could not be parsed as a valid string identifier
[0:] MvxBind:Warning: 17.36 Value '' could not be parsed as a valid string identifier
10-09 19:04:09.002 I/mono-stdout(19422): MvxBind:Warning: 17.36 Value '' could not be parsed as a valid string identifier
[0:] 
MvxBind:Warning: 17.38 Value '' could not be parsed as a valid string identifier
[0:] MvxBind:Warning: 17.38 Value '' could not be parsed as a valid string identifier
10-09 19:04:09.012 I/mono-stdout(19422): MvxBind:Warning: 17.38 Value '' could not be parsed as a valid string identifier
[0:] 
MvxBind:Warning: 17.39 Value '' could not be parsed as a valid string identifier
[0:] MvxBind:Warning: 17.39 Value '' could not be parsed as a valid string identifier
10-09 19:04:09.032 I/mono-stdout(19422): MvxBind:Warning: 17.39 Value '' could not be parsed as a valid string identifier
[0:] 
MvxBind:Warning: 17.40 Value '' could not be parsed as a valid string identifier
[0:] MvxBind:Warning: 17.40 Value '' could not be parsed as a valid string identifier
10-09 19:04:09.042 I/mono-stdout(19422): MvxBind:Warning: 17.40 Value '' could not be parsed as a valid string identifier
[0:] 
MvxBind:Warning: 17.42 Value '' could not be parsed as a valid string identifier
[0:] MvxBind:Warning: 17.42 Value '' could not be parsed as a valid string identifier
10-09 19:04:09.052 I/mono-stdout(19422): MvxBind:Warning: 17.42 Value '' could not be parsed as a valid string identifier

1 个答案:

答案 0 :(得分:1)

解决了这个问题。

喜欢分享我在其他人需要的情况下取得的成就。

我已经简单地更改了StatusToColorValueConverter,如下所示,使其正常工作

public class StatusToColorValueConverter : MvxValueConverter<int,Color>
    {            
        protected override Color Convert(int value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Color TextColor = Color.Gray;
            switch ((int)value)
            {
                case 1:
                    TextColor = Color.Green;//green
                    break;
                case 2:
                    TextColor = Color.Yellow;
                    break;

                case 3:
                    TextColor = Color.Red;
                    break;
                default:
                    TextColor = Color.Gray;
                    break;
            }
            return TextColor;
        }
    }

注意:颜色是Android.Graphics.Color类

现在,简单地调用TextView

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/myCustomId"
        android:textColor="@color/gray_color"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall"
        local:MvxBind="Text DateValue, Converter=FormatDate; TextColor Flag, Converter=StatusToColor" />