设置的LinearLayout的背景颜色,我试图用一个MvxValueConverter设置基于一个布尔值的LinearLayout的背景色。该转换器看起来是这样的:通过布尔值
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;
}
}
在我AXML布局,我有以下代码:
<?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>
,我发现了以下错误:
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 <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
所以,我真的不知道从哪里去。我试图做什么?我是否使用了正确的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;
}
}
...解决我的问题。我的LinearLayout
上也有TextColor MyBooleanValue, Converter=TextColor
,其功能与BackgroundColorValueConverter
类似,我也遇到了关于未能创建目标绑定的相同错误。
有一次,我改变了我的AXML阅读:
<?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>
......一切都按预期。对于任何偶然发生在未来的偶然事件:不要尝试绑定上的TextColor
,因为它不起作用!
以下是我所假设的全部错误消息: http://pastebin.com/24vZpa67 –
啊 - 我看到问题...将更新我的答案(也请编辑您的问题以包含错误跟踪 - 它确实将帮助下一个人) – Stuart
更新了问题,在此先感谢您的帮助! –