設置的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
更新了問題,在此先感謝您的幫助! –