I'm trying to use an MvxValueConverter to set the background color of a LinearLayout based on a boolean value. The converter looks like this:
public class BackgroundColorValueConverter : MvxValueConverter<bool, MvxColor>
{
private static readonly MvxColor TrueBGColor = new MvxColor(0xDB, 0xFF, 0xCE);
private static readonly MvxColor FalseBGColor = new MvxColor(0xD6, 0xF6, 0xFF);
protected override MvxColor Convert(bool value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value ? TrueBGColor : FalseBGColor;
}
}
In my AXML layout, I have the following code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="fill_parent"
local:MvxBind="BackgroundColor MyBooleanValue, Converter=BackgroundColor">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="18dp"
local:MvxBind="Text MyText" />
</LinearLayout>
I'm getting the following error:
Failed to create target binding for binding BackgroundColor for MyBooleanValue
The full error trace is as follows:
MvxBind:Error: 8.58 Problem seen during binding execution for binding BackgroundColor for MyBooleanValue - problem InvalidCastException: Cannot cast from source type to destination type.
03-05 14:18:46.434 I/mono-stdout(16474): MvxBind:Error: 8.58 Problem seen during binding execution for binding BackgroundColor for MyBooleanValue - problem InvalidCastException: Cannot cast from source type to destination type.
03-05 14:18:46.434 I/mono-stdout(16474): at Cirrious.MvvmCross.Plugins.Color.Droid.BindingTargets.MvxViewBackgroundColorBinding.SetValueImpl (System.Object target, System.Object value) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Plugins.Color.Droid.BindingTargets.MvxViewBackgroundColorBinding.SetValueImpl (System.Object target, System.Object value) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.Bindings.Target.MvxConvertingTargetBinding.SetValue (System.Object value) [0x00000] in <filename unknown>:0
03-05 14:18:46.434 I/mono-stdout(16474): at Cirrious.MvvmCross.Binding.Bindings.Target.MvxConvertingTargetBinding.SetValue (System.Object value) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (System.Object value) [0x00000] in <filename unknown>:0
So, I'm honestly not sure where to go from here. Is what I'm attempting possible? Am I using the right MvvmCross converter? Any pointers would be much appreciated.
Update:
Changing the converter to:
public class BackgroundColorValueConverter : MvxColorValueConverter
{
private static readonly MvxColor TrueBGColor = new MvxColor(0xDB, 0xFF, 0xCE);
private static readonly MvxColor FalseBGColor = new MvxColor(0xD6, 0xF6, 0xFF);
protected override MvxColor Convert(object value, object parameter, System.Globalization.CultureInfo culture)
{
return (bool)value ? TrueBGColor : FalseBGColor;
}
}
... resolved my problem. I also had TextColor MyBooleanValue, Converter=TextColor
on my LinearLayout
, which functioned similarly to BackgroundColorValueConverter
, and I was getting the same error about failing to create target bindings.
Once I changed my AXML to read:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="fill_parent"
local:MvxBind="BackgroundColor MyBooleanValue, Converter=BackgroundColor">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="18dp"
local:MvxBind="Text MyText; TextColor MyBooleanValue, Converter=TextColor" />
</LinearLayout>
... everything worked as intended. For anyone who happens to stumble across this in the future: don't try to bind TextColor
on a LinearLayout
, because it doesn't work like that!