MvvmCross:更改MonoDroid上绑定的更新源触发器属性

时间:2013-10-07 03:20:14

标签: data-binding xamarin.android mvvmcross

我想在Droid EditText视图中将PropertyChanged的默认绑定触发器更改为LostFocus:

 <EditText
                android:layout_width="fill_parent"
                android:layout_gravity="center"
                android:textSize="16dp"
                android:minWidth="168dp"
                local:MvxBind="Text SelectedCode, UpdateSourceTrigger=LostFocus" />

但是我找不到Wiki

中的正确语法

我知道这可能在框架内但无法找到参考。

想法?

TIA。

1 个答案:

答案 0 :(得分:7)

绑定语法不提供UpdateSourceTrigger

更改触发机制的唯一方法是:

  • 提供自定义绑定
  • 或提供自定义控件

我会选择自定义绑定 - 例如:

public class MvxEditTextFocusChangeTextSpecialTargetBinding
    : MvxAndroidTargetBinding
{
    protected EditText EditText
    {
        get { return (EditText)Target; }
    }

    private bool _subscribed;

    public MvxEditTextFocusChangeTextSpecialTargetBinding(EditText view)
        : base(view)
    {
    }

    protected override void SetValueImpl(object target, object value)
    {
        var editText = EditText;
        if (editText == null)
            return;

        value = value ?? string.Empty;
        editText.Text = value.ToString();
    }

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

    public override void SubscribeToEvents()
    {
        var editText = EditText;
        if (editText == null)
            return;

        editText.FocusChange += HandleFocusChange;
        _subscribed = true;
    }

    private void HandleFocusChange(object sender, View.FocusChangeEventArgs e)
    {
        var editText = EditText;
        if (editText == null)
            return;

        if (!e.HasFocus)
            FireValueChanged(editText.Text);
    }

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

    protected override void Dispose(bool isDisposing)
    {
        if (isDisposing)
        {
            var editText = EditText;
            if (editText != null && _subscribed)
            {
                editText.FocusChange -= HandleFocusChange;
                _subscribed = false;
            }
        }
        base.Dispose(isDisposing);
    }
}

使用注册:

registry.RegisterCustomBindingFactory<EditText>("FocusText",
                                                        textView => new MvxEditTextFocusChangeTextSpecialTargetBinding(textView));

然后用作:

 local:MvxBind="FocusText VMProperty"

有关自定义绑定的更多信息,请参阅N = 28教程 - http://slodge.blogspot.co.uk/2013/06/n28-custom-bindings-n1-days-of-mvvmcross.html