我已经使用MvvmCross为EditText上的FocusChange事件创建了一个自定义绑定。我可以绑定并触发事件,但我不知道如何传递事件参数。我的自定义绑定是这个
using Android.Views;
using Android.Widget;
using Cirrious.MvvmCross.Binding;
using Cirrious.MvvmCross.Binding.Droid.Target;
using Cirrious.MvvmCross.Binding.Droid.Views;
using Cirrious.MvvmCross.ViewModels;
using System;
namespace MPS_Mobile_Driver.Droid.Bindings
{
public class MvxEditTextFocusChangeBinding
: MvxAndroidTargetBinding
{
private readonly EditText _editText;
private IMvxCommand _command;
public MvxEditTextFocusChangeBinding(EditText editText) : base(editText)
{
_editText = editText;
_editText.FocusChange += editTextOnFocusChange;
}
private void editTextOnFocusChange(object sender, EditText.FocusChangeEventArgs eventArgs)
{
if (_command != null)
{
_command.Execute( eventArgs );
}
}
public override void SetValue(object value)
{
_command = (IMvxCommand)value;
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
_editText.FocusChange -= editTextOnFocusChange;
}
base.Dispose(isDisposing);
}
public override Type TargetType
{
get { return typeof(IMvxCommand); }
}
protected override void SetValueImpl(object target, object value)
{
}
public override MvxBindingMode DefaultMode
{
get { return MvxBindingMode.OneWay; }
}
}
}
我在我的ViewModel中把它连接成这样:
public IMvxCommand FocusChange
{
get
{
return new MvxCommand(() =>
OnFocusChange()
);
}
}
private void OnFocusChange()
{
//Do Something
}
有没有像这样的方法
public IMvxCommand FocusChange
{
get
{
return new MvxCommand((e) =>
OnFocusChange(e)
);
}
}
private void OnFocusChange(EditText.FocusChangeEventArgs e)
{
//Do Something
}
我试图在那里做的事情没有奏效,但我希望有类似的事情可能奏效。当命令在具有以下行的自定义绑定中触发时,我能够传递eventargs
_command.Execute( eventArgs );
我只是想不出在ViewModel中捕捉它们的方法。有人能帮我吗?
Jim
在尝试了许多不同的安排后,我发现连接MvxCommand的正确语法是
public IMvxCommand FocusChange
{
get
{
return new MvxCommand<EditText.FocusChangeEventArgs>(e => OnFocusChange(e));
}
}
private void OnFocusChange(EditText.FocusChangeEventArgs e)
{
if (!e.HasFocus)
{
//Do Something
}
}
希望这能有所帮助!