通过布尔值设置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 ,它的function类似于BackgroundColorValueConverter ,而且我得到了关于未能创build目标绑定的错误。

一旦我把我的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> 

一切都按预期工作 对于以后偶然遇到的问题:不要试图在LinearLayout上绑定TextColor ,因为它不能像这样工作!

Solutions Collecting From Web of "通过布尔值设置LinearLayout背景颜色"

https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/ValueConversion/ValueConversion.UI.Droid/Resources/Layout/View_Colors.axml中有一&#x4E2A;BackgroundColor绑定的工作示例

这使用从https://github.com/MvvmCross/MvvmCross/blob/v3.1/Plugins/Cirrious/Color/Cririous.MvvmCross.Plugins.Color.Droid/BindingTargets/MvxViewBackgroundColorBinding.cs的BackgroundColor绑&#x5B9A;

这个样本是否适合你?

如果是的话,你能发现样本和你正在使用的样本之间的区别吗? Color插件有问题吗? (是否加载到你的UI项目?)这是LinearLayout与TextView的问题? 是否还有更多可以提供的错误跟踪? 您提供的一行跟踪创build在https://github.com/MvvmCross/MvvmCross/blob/1ec7bc5f0307595c7ae11f56727dd0e9d2a2262f/Cirrious/Cirrious.MvvmCross.Binding/Bindings/MvxFullBinding.cs#L139 – 但通常有其他跟踪线。

如果不是,那么这很令人担忧,因为这意味着它是一个普通的bug。


更新:(更多信息提供后)

我认为问题出在你的ValueConverter上 – 要使用Android,你的ValueConverter必须以Nativetypes – 而不是独立于平台的MvxColor 。 你看到的错误是一个无效的转换exception – 因为绑定试图将你的MvxColor转换为https://github.com/MvvmCross/MvvmCross/blob/v3.1/Plugins/Cirrious/中&#x7684;Android.Graphics.Color 颜色/ Cirrious.MvvmCross.Plugins.Color.Droid / BindingTargets / MvxViewBackgroundColorBinding.cs#L25

要转换为Native,可以使用MvxColorValueConverter基类 – 请参阅https://github.com/MvvmCross/MvvmCross/blob/v3.1/Plugins/Cirrious/Color/Cririous.MvvmCross.Plugins.Color/MvxColorValueConverter.cs

其中一个例子是https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/ValueConversion/ValueConversion.Core/Converters/Converters.cs#L35

 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上有一些关于颜色转换器的文&#x6863;

你正在返回一个布尔值在你的方法,它期待一个MvxColor对象。

  protected override MvxColor Convert(object value, object parameter, System.Globalization.CultureInfo culture) { (bool)value ? return TrueBGColor : return FalseBGColor; } 

面对“无法创build目标绑定…”相同的错误,只需将转换器从core-PCL项目下的文件夹移动到Droid项目下的新文件夹做了我的伎俩:)