在 MvvmCross 中如何自定义绑定属性 [英] In MvvmCross how do I do custom bind properties

查看:28
本文介绍了在 MvvmCross 中如何自定义绑定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MvxBindableListView 将数据对象的 List<> 绑定到 ListView.我用于行的布局有几个 TextView .我成功地将每个这些的 Text 属性绑定到我的数据对象中的一个属性,但我发现我无法绑定到 TextColor 因为该属性不存在于Mono For Android TextViews;相反,您使用了 SetTextColor() 方法.那么如何将数据对象属性绑定到方法呢?!以下是我尝试使用的代码:

I am using MvxBindableListView to bind a List<> of data objects to a ListView. The layout I am using for the rows has several TextViews. I am successfully binding the Text property for each of these to a property in my data object, but I have found that I cannot bind to TextColor as that property does not exist in Mono For Android TextViews; instead you have use the SetTextColor() method. So how can I bind a data object property to a method?! Below is the code I tried to use:

    <TextView
        android:id="@+id/MyValueTextView"
        android:layout_width="50dp"
        android:layout_height="20dp"
        android:layout_gravity="right"
        android:gravity="center_vertical|right"
        android:textSize="12sp"
        local:MvxBind="
        {
          'Text':{'Path':'MyValue','Converter':'MyValueConverter'},
          'TextColor':{'Path':'MyOtherValue','Converter':'MyOtherConverter'}
        }" />

推荐答案

在会议示例中有一个为IsFavorite"添加自定义 2 路绑定的示例 - 请参阅:

There's an example of adding a custom 2-way binding for "IsFavorite" in the Conference sample - see:

  • the binding - https://github.com/slodge/MvvmCross/blob/master/Sample%20-%20CirriousConference/Cirrious.Conference.UI.Droid/Bindings/FavoritesButtonBinding.cs
  • the binding setup in FillTargetFactories in https://github.com/slodge/MvvmCross/blob/master/Sample%20-%20CirriousConference/Cirrious.Conference.UI.Droid/Setup.cs

此示例在以下内容中进一步解释:Android 中的 MVVMCross 绑定

This example is explained a bit further in: MVVMCross Bindings in Android

对于单向源到目标"的自定义绑定,代码应该简单一点——你只需要处理SetValue——不需要调用FireValueChanged 在任何事件处理代码中.

For a one-way "source-to-target" custom binding, the code should be a bit simpler - you only need to handle the SetValue - and don't need to invoke FireValueChanged in any event handling code.

对于 textColor,我想绑定看起来有点像:

For textColor, I'd imagine the binding would look a bit like:

public class MyCustomBinding
    : MvxBaseAndroidTargetBinding
{
    private readonly TextView _textView;

    public MyCustomBinding(TextView textView)
    {
        _textView = textView;
    }

    public override void SetValue(object value)
    {
        var colorValue = (Color)value;
        _textView.SetTextColor(colorValue);
    }

    public override Type TargetType
    {
        get { return typeof(Color); }
    }

    public override MvxBindingMode DefaultMode
    {
        get { return MvxBindingMode.OneWay; }
    }
}

并且将设置为:

    protected override void FillTargetFactories(MvvmCross.Binding.Interfaces.Bindings.Target.Construction.IMvxTargetBindingFactoryRegistry registry)
    {
        base.FillTargetFactories(registry);

        registry.RegisterFactory(new MvxCustomBindingFactory<TextView>("TextColor", (textView) => new MyCustomBinding(textView)));
    }

注意:我没有编译这个示例代码 - 当你让它工作时,请回来更正这个伪代码:)

Note: I've not compiled this example code - when you do get it working, please come back and correct this pseudo-code :)

这篇关于在 MvvmCross 中如何自定义绑定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆