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

    <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 向绑定的示例 - 请参阅:

这个例子在下面有进一步的解释: Android 中的 MVVM 交叉绑定

对于单向“源到目标”自定义绑定,代码应该更简单一些 - 您只需要处理 SetValue - 并且不需要调用 FireValueChanged 在任何事件处理代码中。


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

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)));
    }

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top