c# - MvvmCross 自定义事件绑定(bind)事件参数

标签 c# android xamarin mvvmcross mvxbind

我已经使用 MvvmCross 在 EditText 上为 FocusChange 事件创建了自定义绑定(bind)。我可以绑定(bind)并触发事件,但我不知道如何传递事件参数。我的自定义绑定(bind)是这样的

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
}

我在那里尝试做的事情没有用,但我希望有类似的东西可能有用。当命令在使用此行的自定义绑定(bind)中触发时,我能够传递 eventargs

            _command.Execute( eventArgs );

我只是想不出在 ViewModel 中捕获它们的方法。谁能帮我解决这个问题?

吉姆

最佳答案

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

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

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

希望这对您有所帮助!

关于c# - MvvmCross 自定义事件绑定(bind)事件参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29292364/

相关文章:

c# - 如何在不序列化的情况下 Redis 缓存大型 C# 对象?

android - Xamarin Android "ScrollView"奇怪的行为

c# - 具有私有(private) setter 的 UWP DependencyProperty

c# - 将实体(和相应的表)添加到 EF DB-first 模型

android - 如何跨应用程序限制内容提供者数据

Android GPS获取当前位置

android - 我们如何将 Flurry 添加到 AdMobs 或 AdWhirl Mediation?

android - 分组推送通知再次调用 MainActivity 的 OnCreate()。如何处理?

c# - 如何从 TableView 传递数据? (Xamarin.iOS)

c# - GAC 中的 .Net 静态变量和 DLL 版本