我正在尝试使用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;
}
}
...解决了我的问题。我的TextColor MyBooleanValue, Converter=TextColor
上也有LinearLayout
,其功能与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
上绑定LinearLayout
,因为它不能像那样工作!
答案 0 :(得分:3)
BackgroundColor
绑定的工作示例
中的BackgroundColor绑定
此示例是否适合您?
如果是,您能否发现该样本与您正在使用的样本之间的差异? Color插件有问题吗? (它是否在您的UI项目中加载?)LinearLayout与TextView的问题是什么?您可以提供更多错误跟踪吗?您提供的一行跟踪是在https://github.com/MvvmCross/MvvmCross/blob/1ec7bc5f0307595c7ae11f56727dd0e9d2a2262f/Cirrious/Cirrious.MvvmCross.Binding/Bindings/MvxFullBinding.cs#L139上创建的 - 但在该行之前通常还有其他跟踪。
如果不是,那就令人担心,因为这意味着它是一个普遍的错误......
更新:(提供更多信息后)
我认为问题在于你的ValueConverter - 要使用Android,你的ValueConverter必须以Native类型结束 - 而不是与平台无关的MvxColor
。您看到的错误是无效的强制转换异常 - 因为绑定正试图将您的MvxColor
强制转换为https://github.com/MvvmCross/MvvmCross/blob/v3.1/Plugins/Cirrious/Color/Cirrious.MvvmCross.Plugins.Color.Droid/BindingTargets/MvxViewBackgroundColorBinding.cs#L25中的Android.Graphics.Color
要转换为Native,您可以使用MvxColorValueConverter
基类 - 请参阅https://github.com/MvvmCross/MvvmCross/blob/v3.1/Plugins/Cirrious/Color/Cirrious.MvvmCross.Plugins.Color/MvxColorValueConverter.cs
public class ContrastColorConverter : MvxColorValueConverter
{
protected override MvxColor Convert(object value, object parameter, CultureInfo culture)
{
var input = (MvxColor) value;
var brightnessToUse = SimpleContrast(input.R, input.G, input.B);
return new MvxColor(brightnessToUse, brightnessToUse, brightnessToUse);
}
private static int SimpleContrast(params int[] value)
{
// this is only a very simple contrast method
// for more advanced methods you need to look at HSV-type approaches
int max = 0;
foreach (var v in value)
{
if (v > max)
max = v;
}
return 255 - max;
}
}
https://github.com/MvvmCross/MvvmCross/wiki/Value-Converters#wiki-the-mvx-color-valueconverters
中有一些关于颜色转换器的文档答案 1 :(得分:0)
你在你的方法上返回一个布尔值,它期待一个MvxColor对象。
protected override MvxColor Convert(object value, object parameter, System.Globalization.CultureInfo culture)
{
(bool)value ? return TrueBGColor : return FalseBGColor;
}
答案 2 :(得分:0)
面临同样的错误&#34;无法创建目标绑定...&#34;,只需将转换器从core-PCL项目下的文件夹移动到Droid项目下的新文件夹就可以了解我:)