Can´t get MVVMCross MvxColorValueConverter to work on a TextView TextColor

I have created a custom color converter like this

public class TextColorConverter : MvxColorValueConverter
{
private static readonly MvxColor ErrorTextColor = new MvxColor(0xFF, 0x00, 0x00);
private static readonly MvxColor StandardTextColor = new MvxColor(0x0, 0x0, 0x0);

    protected override MvxColor Convert(object value, object parameter, System.Globalization.CultureInfo culture)
    {
        return (bool)value ? ErrorTextColor : StandardTextColor ;
    }
}

and I set up the binding like this.
local:MvxBind="TextColor IsTaskDelayed, Converter=TextColorConverter"

I get the following error:

MvxBind:Error: 24,42 Problem seen during binding execution for binding TextColor for IsTaskDelayed - problem InvalidCastException: Specified cast is not valid.
at MvvmCross.Plugins.Color.Droid.BindingTargets.MvxTextViewTextColorBinding.SetValueImpl (System.Object target, System.Object value) [0x0000a] in :0
at Cirrious.MvvmCross.Binding.Bindings.Target.MvxConvertingTargetBinding.SetValue (System.Object value) [0x00088] in :0
at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (System.Object value) [0x00024] in :0

Anyone have a solution to this ??

Best Answer

Answers

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    The "TextColor" property is expecting a Android.Graphics.Color type, but your ValueConverter is returning an MvxColor. That is why you're getting an InvalidCastException. You need to be returning an Android.Graphics.Color.

    using Android.Graphics;
    public class ColorValueConverter : MvxValueConverter<bool, Color>
    {
      private static readonly Color ErrorTextColor = new Color(0xFF, 0x00, 0x00);
      private static readonly Color StandardTextColor = new Color(0x0, 0x0, 0x0);
    
      protected override Color Convert (bool value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
        return value ? ErrorTextColor : StandardTextColor ;
      }
    }
    

    Also note that this value converter must be defined in your Android Project. For your iOS project, you'll have to create a similiar value converter that returns an UIColor.

  • HenrikStrmbergHenrikStrmberg USMember ✭✭

    Thanks for your tip but it still don´t work. <The convert function is never called. Do I need to register the converter ?

  • Martijn00Martijn00 NLInsider, University ✭✭✭

    MvvmCross uses Coc, which means that a converter with the name MvxColorValueConverter must be used as MvxColor in your code.

  • HenrikStrmbergHenrikStrmberg USMember ✭✭

    @Martijn00 not sure what you meen coule you explain a bit more ?

  • potato12321potato12321 AUMember

    having the same issue here. the converter is never called when trying to convert DateTime to string. any update?

  • potato12321potato12321 AUMember
    edited October 2016

    Found out the solution, it is embarrassing actually... so I was missing the "public" keyword when declaring the class in "public class DateToStringValueConverter : MvxValueConverter<DateTime, string>".

    Hope this gives hint to others facing similar issue :) #copyPasteEffect

    namespace MyProject.Converters
    {
    public class DateToStringValueConverter : MvxValueConverter<DateTime, string>
    {

        protected override string Convert(DateTime value, Type targetType, object parameter, CultureInfo culture)
        {
    
            return "string";
        }       
    
    }
    

    }

Sign In or Register to comment.