Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MvvmCross: change update source trigger property of binding on MonoDroid

I will like to change the default binding trigger from PropertyChanged to LostFocus on a Droid EditText view:

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

But I cannot find the correct syntax from the Wiki

I know this is possible within the framework but cannot find the reference.

Ideas?

TIA.

like image 614
Klaus Nji Avatar asked Mar 23 '23 03:03

Klaus Nji


1 Answers

The binding syntax doesn't provide UpdateSourceTrigger

The only ways to change the triggering mechanism are:

  • to provide a custom binding
  • or to provide a custom control

I'd go for custom binding - something like:

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

registered using:

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

then used as:

 local:MvxBind="FocusText VMProperty"

For more on custom bindings, see the N=28 tutorial - http://slodge.blogspot.co.uk/2013/06/n28-custom-bindings-n1-days-of-mvvmcross.html

like image 179
Stuart Avatar answered Apr 06 '23 17:04

Stuart