MvvmCross自定义事件绑定事件Args

时间:2015-03-27 02:34:53

标签: c# android xamarin mvvmcross mvxbind

我使用MvvmCross在EditText上为FocusChange事件创建了自定义绑定。我可以得到事件绑定和解雇,但我无法弄清楚如何传递事件args。我的自定义绑定就是这个

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中捕获它们的方法。任何人都可以帮我这个吗?

吉姆

1 个答案:

答案 0 :(得分:3)

在尝试了许多不同的安排后,我发现连接MvxCommand的正确语法是

public IMvxCommand FocusChange
{
    get
    {
        return new MvxCommand<EditText.FocusChangeEventArgs>(e => OnFocusChange(e));
    }
}

private void OnFocusChange(EditText.FocusChangeEventArgs e)
{
    if (!e.HasFocus)
    {
         //Do Something
    }
}

希望这有帮助!