Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
706 views
in Technique[技术] by (71.8m points)

xamarin.android - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...