MvvmCross Custom Event Binding Event Args

白昼怎懂夜的黑 提交于 2019-12-10 18:49:07

问题


I have created a custom binding for the FocusChange event on an EditText using MvvmCross. I can get the event bound and firing, but I can't figure out how to pass the event args. My Custom Binding is this

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; }
        }
    }
}

I wire it up in my ViewModel as this:

public IMvxCommand FocusChange
{
    get
    {
        return new MvxCommand(() =>
            OnFocusChange()
            );
    }
}

private void OnFocusChange()
{
    //Do Something
}

Is there a way to do something like

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

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

What I tried to do there doesn't work, but I was hoping that there was something similar that might work. I am able to pass the eventargs when the command fires in the custom binding with this line

            _command.Execute( eventArgs );

I just can't figure a way to catch them in the ViewModel. Can anyone help me with this?

Jim


回答1:


After trying many different arrangements, I found that the correct syntax to wire up your MvxCommand is

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

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

Hope this helps!



来源:https://stackoverflow.com/questions/29292364/mvvmcross-custom-event-binding-event-args

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!