通过布尔值设置LinearLayout的背景颜色

8

我正在尝试使用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>

...所有的内容都按照预期运行。对于未来可能偶然遇到这个问题的人:不要尝试在LinearLayout上绑定TextColor,因为它不起作用!

3个回答

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

这个示例是否对您有效?

如果有效,您能否发现该示例和您正在使用的示例之间的区别?这是颜色插件的问题吗?(它是否加载在您的UI项目中?)是LinearLayout与TextView之间的问题吗?您可以提供更多错误跟踪信息吗? 您提供的一行跟踪信息是在https://github.com/MvvmCross/MvvmCross/blob/1ec7bc5f0307595c7ae11f56727dd0e9d2a2262f/Cirrious/Cirrious.MvvmCross.Binding/Bindings/MvxFullBinding.cs#L139创建的-但通常在该行之前会有其他跟踪信息。

如果无效,则说明这是一个一般性的错误...


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

我认为问题在于您的ValueConverter-为了与Android配合使用,您的ValueConverter必须最终以本机类型结束-而不是平台无关的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

要转换为本地格式,可以使用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上有一些关于颜色转换器的文档。


啊,我明白问题了...我会更新我的答案(请同时编辑您的问题以包含该错误跟踪 - 这确实会帮助下一个人)。 - Stuart
更新了问题,提前感谢您的帮助! - Walter W Kennedy IV
谢谢。很抱歉打扰了,但请将跟踪信息放在问题中而不是pastebin中 - 将跟踪信息放在问题中将有助于搜索优化,并避免将来可能出现的跟踪丢失问题。抱歉 <- 我的StackOverflow强迫症发作 :) - Stuart
谢谢,Stuart!我已经更新了我的问题,包括错误跟踪以及我注意到的关于LinearLayout上的TextColor的一些事情(以及对于像我这样的笨蛋它不起作用)。 - Walter W Kennedy IV

0

你的方法应该返回一个MvxColor对象,但你却返回了一个布尔值。

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

我更新了我的问题。但我不认为那是问题所在 - 它基于 value 的值返回 TrueBGColorFalseBGColor - Walter W Kennedy IV

0

遇到了关于“无法创建目标绑定”的相同错误,将转换器从core-PCL项目下的文件夹移动到Droid项目下的新文件夹中对我起了作用 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接