Wal*_* IV 8 android converter mvvmcross xamarin
我正在尝试使用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;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的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>
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Failed to create target binding for binding BackgroundColor for MyBooleanValue
Run Code Online (Sandbox Code Playgroud)
完整的错误跟踪如下:
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
Run Code Online (Sandbox Code Playgroud)
所以,老实说我不确定从哪里开始.我正在尝试的是什么?我使用正确的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;
}
}
Run Code Online (Sandbox Code Playgroud)
......解决了我的问题.我也有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>
Run Code Online (Sandbox Code Playgroud)
......一切按预期工作.对于任何人谁发生在未来过这个跌倒:不要试图绑定TextColor上LinearLayout,因为它不喜欢的工作!
https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/ValueConversion/ValueConversion.UI.Droid/Resources/Layout/View_Colors.axmlBackgroundColor中有一个绑定的工作示例
这使用来自https://github.com/MvvmCross/MvvmCross/blob/v3.1/Plugins/Cirrious/Color/Cirrious.MvvmCross.Plugins.Color.Droid/BindingTargets/MvxViewBackgroundColorBinding.cs的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.MvvmCrossAndroid.Graphics.Color中的。 Plugins.Color.Droid/BindingTargets/MvxViewBackgroundColorBinding.cs#L25
要转换为 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;
}
}
Run Code Online (Sandbox Code Playgroud)
https://github.com/MvvmCross/MvvmCross/wiki/Value-Converters#wiki-the-mvx-color-valueconverters有一些关于颜色转换器的文档