我有一个像这样的editText:
<EditText
local:MvxBind="Text MyProperty; Click MyCommand"
android:inputType="textShortMessage"
android:imeOptions="actionSend" />
我希望命令由键盘上的“输入/操作”键触发。我应该在绑定中使用什么动词? “改变”动词我当前在控件中的任何触摸上使用触发器!(
答案 0 :(得分:5)
如果要实现“自定义绑定”,则有3种方法可以执行此操作:
https://stackoverflow.com/a/19221385/373321中的答案显示了真正的自定义绑定。
继承自标准视图的自定义控件在http://mvvmcross.blogspot.com中的N = 18中讨论 - 这里的示例可能是:
public class DoneEditText : EditText, TextView.IOnEditorActionListener
{
public DoneEditText(Context context) : base(context)
{
Init();
}
public DoneEditText(Context context, IAttributeSet attrs) : base(context, attrs)
{
Init();
}
public DoneEditText(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
Init();
}
private void Init()
{
this.SetOnEditorActionListener(this);
}
public ICommand DoneAction { get;set; ]
#region Implementation of IOnEditorActionListener
public bool OnEditorAction(TextView v, ImeAction actionId, KeyEvent e)
{
if (actionId == ImeAction.Done)
{
if (DoneAction != null)
DoneAction.Execute(v.Text);
return true;
}
return false;
}
#endregion
}
使用中间对象进行绑定将使用类似的类:
public class DoneActionListener : Java.Lang.Object, TextView.IOnEditorActionListener
{
public ICommand DoneAction { get; set; }
public DoneActionListener(TextView v)
{
v.SetOnEditorActionListener(this);
}
#region Implementation of IOnEditorActionListener
public bool OnEditorAction(TextView v, ImeAction actionId, KeyEvent e)
{
if (actionId == ImeAction.Done)
{
if (DoneAction != null)
DoneAction.Execute(v.Text);
return true;
}
return false;
}
#endregion
}
这可以使用Fluent Binding在OnCreate
中处理,如:
private DoneActionListener _listener;
public override OnCreate(Bundle b)
{
base.OnCreate(b);
SetContentView(Resource.Layout.MyLayoutId);
_listener = new DoneActionListener(FindViewById<EditText>(Resource.Id.MyEditId));
var set = this.CreateBindingSet<MyView, MyViewModel>();
set.Bind(_listener).For(v => v.DoneAction).To(vm => vm.TheDoneCommand);
set.Apply();
}