2014-03-05 33 views
8

设置的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,因为它不起作用!

回答

3

还有的的BackgroundColor工作样品中https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/ValueConversion/ValueConversion.UI.Droid/Resources/Layout/View_Colors.axml

结合本品采用BACKGROUNDCOLOR从https://github.com/MvvmCross/MvvmCross/blob/v3.1/Plugins/Cirrious/Color/Cirrious.MvvmCross.Plugins.Color.Droid/BindingTargets/MvxViewBackgroundColorBinding.cs

结合为你做这项工作的样本?

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

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


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

我认为这个问题是在你的ValueConverter - 与Android合作,您ValueConverter与本机类型来结束 - 而不是与平台独立MvxColor。您所看到的错误是一个无效的转换异常 - 因为绑定试图投你MvxColorAndroid.Graphics.Colorhttps://github.com/MvvmCross/MvvmCross/blob/v3.1/Plugins/Cirrious/Color/Cirrious.MvvmCross.Plugins.Color.Droid/BindingTargets/MvxViewBackgroundColorBinding.cs#L25

要转换到本机,可以使用MvxColorValueConverter基类 - 看到https://github.com/MvvmCross/MvvmCross/blob/v3.1/Plugins/Cirrious/Color/Cirrious.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

+0

以下是我所假设的全部错误消息: http://pastebin.com/24vZpa67 –

+0

啊 - 我看到问题...将更新我的答案(也请编辑您的问题以包含错误跟踪 - 它确实将帮助下一个人) – Stuart

+0

更新了问题,在此先感谢您的帮助! –

0

你对你的方法返回一个boolean值,其中它的期待MvxColor对象。

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

我我更新了我的问题。我不认为这是问题,但它会根据value的值返回TrueBGColor或FalseBGColor。 –

0

面临着同样的错误关于“无法创建目标结合...”,干脆搬到从文件夹中的核心-PCL项目下的转换器下的Droid项目的新文件夹的伎俩对我来说:)