问题描述
我想在 Droid EditText 视图上将默认绑定触发器从 PropertyChanged 更改为 LostFocus:
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" />
但我无法从 Wiki
我知道这在框架内是可能的,但找不到参考.
I know this is possible within the framework but cannot find the reference.
想法?
TIA.
推荐答案
绑定语法不提供UpdateSourceTrigger
改变触发机制的唯一方法是:
The only ways to change the triggering mechanism are:
- 提供自定义绑定
- 或提供自定义控件
我会选择自定义绑定 - 类似于:
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);
}
}
注册使用:
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
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
这篇关于MvvmCross:更改 MonoDroid 上绑定的更新源触发器属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!