Search code examples
androidxamarinxamarin.androidmvvmcross

Multiple Value Converter on TextView using MVVMCROSS


I have being struggling in using two value converter on single TextView

Basically I wanted to format the text and change the TextColor based on certain value.

Below is how I am trying to achieve this.

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

The following are my two converters

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

Both the converters are getting called but I am not getting any value inside the text view.

can any one please guide to solve this.

Thanks Aaman

============================ Following is the stack trace for the same

[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

Solution

  • Resolved the issue.

    Like to share how I have achieved in case any one else require.

    I have simple changed the StatusToColorValueConverter Like below to make it working

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

    Note: Color is Android.Graphics.Color class

    Now, Simply Call on the 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" />