【问题标题】:mvvmcross MvxBind on actionSend (imeOption action)mvvmcross MvxBind on actionSend (imeOption action)
【发布时间】:2013-12-30 11:04:31
【问题描述】:

我有一个这样的editText:

    <EditText
        local:MvxBind="Text MyProperty; Click MyCommand"
        android:inputType="textShortMessage"
        android:imeOptions="actionSend" />

我希望命令由键盘上的“输入/操作”键触发。我应该在绑定中使用什么动词?我目前在控件中的任何触摸上使用的“更改”动词!(

【问题讨论】:

  • 其实不是。他希望在失去焦点时触发一些东西。我不:) 我需要一个自定义转换器吗?什么都没有?如果需要我会写的。但是如果我想将它添加为贡献,它应该放在哪个 mvvmcross 文件/文件夹中?

标签: binding xamarin.android mvvmcross mvxbind


【解决方案1】:

如果你想实现“自定义绑定”,那么有 3 种方法可以做到:

  1. 实现并注册真正的自定义绑定
  2. 实现一个自定义控件,该控件公开一个要绑定的自定义属性
  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
}

这可以在OnCreate 中使用 Fluent Binding 处理,例如:

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

【讨论】:

  • 是否可以使用 MvxAndroidTargetBinding 代替?使用 ICommand 类型的 TargetType ?
  • 我尝试了解决方案1,自定义绑定中没有ICommand绑定支持(除了调试,我还检查了源代码)。所以解决方案1不是一种方法。或者我错过了什么。也许它不是 MvxAndroidTargetBinding。在此类中,您永远无法直接访问“ViewModelTarget”属性。并且不能触发 ICommand 目标。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-19
  • 1970-01-01
  • 2017-05-06
  • 2021-08-28
  • 1970-01-01
  • 2013-05-31
相关资源
最近更新 更多