MVVMCross ValueConverter Bool To MvxColor / Color

时间:2016-06-09 14:34:14

标签: c# xamarin xamarin.android converter mvvmcross

我正在尝试为MVVMCross实现ValueConverter,但显然它没有像我期望的那样工作。所以此刻我刚刚获得了另一个属性,它为我做了转换(MvxColor到TextColor)并将其绑定到我的布局。 但是,我宁愿使用价值转换器,所以也许你知道我做错了什么:

我目前得到的绑定错误:

    06-09 16:29:29.820 I/MvxBind (16312):  61,64 Problem seen during binding execution for binding TextColor for Changed - problem InvalidCastException: Specified cast is not valid.
06-09 16:29:29.820 I/MvxBind (16312):     at MvvmCross.Plugins.Color.Droid.BindingTargets.MvxTextViewTextColorBinding.SetValueImpl (System.Object target, System.Object value) [0x0000a] in <filename unknown>:0 
06-09 16:29:29.820 I/MvxBind (16312):   at MvvmCross.Binding.Bindings.Target.MvxConvertingTargetBinding.SetValue (System.Object value) [0x00088] in <filename unknown>:0 
06-09 16:29:29.820 I/MvxBind (16312):   at MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (System.Object value) [0x00024] in <filename unknown>:0 
MvxBind:Error: 61,64 Problem seen during binding execution for binding TextColor for Changed - problem InvalidCastException: Specified cast is not valid.
06-09 16:29:29.820 I/mono-stdout(16312): MvxBind:Error: 61,64 Problem seen during binding execution for binding TextColor for Changed - problem InvalidCastException: Specified cast is not valid.
06-09 16:29:29.820 I/mono-stdout(16312):      at MvvmCross.Plugins.Color.Droid.BindingTargets.MvxTextViewTextColorBinding.SetValueImpl (System.Object target, System.Object value) [0x0000a] in <filename unknown>:0 
      at MvvmCross.Plugins.Color.Droid.BindingTargets.MvxTextViewTextColorBinding.SetValueImpl (System.Object target, System.Object value) [0x0000a] in <filename unknown>:0 
  at MvvmCross.Binding.Bindings.Target.MvxConvertingTargetBinding.SetValue (System.Object value) [0x00088] in <filename unknown>:0 
06-09 16:29:29.830 I/mono-stdout(16312):   at MvvmCross.Binding.Bindings.Target.MvxConvertingTargetBinding.SetValue (System.Object value) [0x00088] in <filename unknown>:0 
  at MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (System.Object value) [0x00024] in <filename unknown>:0 
06-09 16:29:29.830 I/mono-stdout(16312):   at MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (System.Object value) [0x00024] in <filename unknown>:0 
[0:] MvxBind:Error: 61,64 Problem seen during binding execution for binding TextColor for Changed - problem InvalidCastException: Specified cast is not valid.
      at MvvmCross.Plugins.Color.Droid.BindingTargets.MvxTextViewTextColorBinding.SetValueImpl (System.Object target, System.Object value) [0x0000a] in <filename unknown>:0 
  at MvvmCross.Binding.Bindings.Target.MvxConvertingTargetBinding.SetValue (System.Object value) [0x00088] in <filename unknown>:0 
  at MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (System.Object value) [0x00024] in <filename unknown>:0 

My Layout-Snippet:

<EditText
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:singleLine="true"
      android:inputType="numberDecimal|numberSigned"
      android:textColor="@color/schaefflerlightgreen"
      local:MvxBind="TextColor Changed,Converter=MvxBoolToColorValueConverter" />

我尝试了两个转换器类:

    public class MvxBoolToMvxColorValueConverter: MvxColorValueConverter<bool>
{

    protected override MvxColor Convert(bool value, object parameter, CultureInfo culture)
    {
        if (value)
        {
            // FF0000
            return new MvxColor(255, 0, 0);
        }
        //227D41
        return new MvxColor(34, 125, 65);
    }
}

    public class MvxBoolToColorValueConverter : MvxValueConverter<bool, Color>
{

    protected override Color Convert(bool value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value)
        {
            // FF0000
            return new Color(255, 0, 0);
        }
        //227D41
        return new Color(34, 125, 65);
    }

}

事情是绑定通常使用MvxColor-Property,只是转换似乎是一个问题。所以我可能只是错过了一些东西。

非常感谢你的帮助。

伊迪丝:财产......

public bool Changed { get { return m_sValue != m_sSyncValue; } }

1 个答案:

答案 0 :(得分:4)

从绑定中的转换器名称中删除Mvx - 前缀和ValueConverter - 后缀:

更改

local:MvxBind="TextColor Changed,Converter=MvxBoolToColorValueConverter" />

local:MvxBind="TextColor Changed,Converter=BoolToColor" />

documentation for ValueConverters

中描述了此行为
  

此扫描定位实现IMvxValueConverter的所有可实现的类   在程序集中创建一个实例,每个实例注册一个   名称剥去任何Mvx前缀和任何名称的实例   ValueConverter或转换器后缀。因此,例如,以下内容   类名将全部使用相同的ValueConverter名称注册   &#34; Foo&#34;:

     

Foo,FooValueConverter,FooConverter,MvxFooValueConverter,   MvxFooConverter