我正在尝试使用MvxValueConverter根据布尔值设置LinearLayout的背景颜色.转换器看起来像这样:
public class BackgroundColorValueConverter : MvxValueConverter{ 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; } }
在我的AXML布局中,我有以下代码:
我收到以下错误:
Failed to create target binding for binding BackgroundColor for MyBooleanValue
完整的错误跟踪如下:
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:0 at Cirrious.MvvmCross.Plugins.Color.Droid.BindingTargets.MvxViewBackgroundColorBinding.SetValueImpl (System.Object target, System.Object value) [0x00000] in :0 at Cirrious.MvvmCross.Binding.Bindings.Target.MvxConvertingTargetBinding.SetValue (System.Object value) [0x00000] in :0 03-05 14:18:46.434 I/mono-stdout(16474): at Cirrious.MvvmCross.Binding.Bindings.Target.MvxConvertingTargetBinding.SetValue (System.Object value) [0x00000] in :0 at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (System.Object value) [0x00000] in :0
所以,老实说我不确定从哪里开始.我正在尝试的是什么?我使用正确的MvvmCross转换器吗?任何指针都将非常感激.
更新:
将转换器更改为:
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; } }
......解决了我的问题.我也有TextColor MyBooleanValue, Converter=TextColor
我的LinearLayout
,其功能类似于BackgroundColorValueConverter
,我得到了关于未能创建目标绑定的相同错误.
一旦我将AXML更改为:
......一切按预期工作.对于任何人谁发生在未来过这个跌倒:不要试图绑定TextColor
上LinearLayout
,因为它不喜欢的工作!