简体   繁体   English

带有Mvvmcross的Xamarin Android。 在axml中绑定OnEditorAction

[英]Xamarin Android with Mvvmcross. Binding an OnEditorAction in axml

The situation : I have a list of view which contains an EditText. 情况:我有一个包含EditText的视图列表。 I want the user to be able to modify the text and only send his new text to the viewmodel when he press on the done button on the keyboard. 我希望用户能够修改文本,并且仅当他按键盘上的完成按钮时才将其新文本发送到视图模型。 My edit text is binded has follow 我的编辑文字已绑定

<EditText
   android:id="@+id/textNumero"
   android:layout_width="0dp"
   android:layout_height="fill_parent"
   android:layout_weight="0.23"
   android:textColor="#ffffffff"
   local:MvxBind="Text BarilId" />

I found on the internet that i can you use this event to do what i want: 我在互联网上发现我可以使用此事件来做我想做的事情:

idEditText.OnEditorAction (ImeAction.Done) += //insert delegate here

Unfortunately, i can't reach my activity since i'm binding the class in my list. 不幸的是,由于我将班级绑定到列表中,因此我无法参加活动。

So, i thought of binding a command in my class like this: 因此,我想像这样在我的班级中绑定一个命令:

<EditText
       android:id="@+id/textNumero"
       android:layout_width="0dp"
       android:layout_height="fill_parent"
       android:layout_weight="0.23"
       android:textColor="#ffffffff"
       local:MvxBind="OnEditorAction EditCommand"
       local:MvxBind="Text BarilId" />

The command : 命令 :

    private IMvxCommand _editCommand;
    public IMvxCommand EditCommand
    {
        get {
            _editCommand = _editCommand ?? new MvxCommand(() => {
                //do validation here
            });
            return _editCommand;
        }
    }

But I don't know how to pass the ImeAction.Done to my command or if i even receiving something like that. 但是我不知道如何将ImeAction.Done传递给我的命令,或者我什至没有收到类似的信息。

Can i have some help on the remaining part? 我可以在其余部分上寻求帮助吗?

You could create your custom EditText that has a command and then you could bind it in the axml. 您可以创建具有命令的自定义EditText,然后可以将其绑定到axml中。

The other option would be using: 另一种选择是使用:

idEditText.OnEditorAction (ImeAction.Done) += (ViewModel as MyViewModel).EditCommand.Execute(whateveryouwanttouse);

To reach the Activity you could use: 要进行活动,您可以使用:

Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity

Finally, i use Stuart answer in this : https://stackoverflow.com/a/19221385/1188639 最后,我在此使用Stuart答案: https : //stackoverflow.com/a/19221385/1188639

I just changed the code a little so i can use my OnEditorAction in the SubscibeToEvents() and change the handle function too. 我只是稍微更改了代码,所以可以在SubscibeToEvents()中使用OnEditorAction并更改handle函数。 The whole class look like this : 整个类看起来像这样:

public class MvxEditTextEditForBinding: MvxAndroidTargetBinding
{
    protected EditText EditText
    {
        get { return (EditText)Target; }
    }

    private bool _subscribed;

    public MvxEditTextEditForBinding(EditText view)
        : base(view)
    {
    }

    protected override void SetValueImpl(object target, object value)
    {
        var editText = EditText;
        if (editText == null)
            return;

        value = value ?? string.Empty;
        editText.Text = value.ToString();
    }

    public override MvxBindingMode DefaultMode
    {
        get { return MvxBindingMode.TwoWay; }
    }

    public override void SubscribeToEvents()
    {
        var editText = EditText;
        if (editText == null)
            return;

        editText.EditorAction += HandleEditForChange;
        _subscribed = true;
    }

    private void HandleEditForChange(object sender, TextView.EditorActionEventArgs  e)
    {
        var editText = EditText;

        InputMethodManager imm = (InputMethodManager)editText.Context.GetSystemService(Context.InputMethodService); 
        imm.HideSoftInputFromWindow(editText.WindowToken, 0);


        e.Handled = false;
        if (e.ActionId == ImeAction.Done)
        {
            FireValueChanged(editText.Text);
            e.Handled = true;   
        }
    }

    public override Type TargetType
    {
        get { return typeof(string); }
    }

    protected override void Dispose(bool isDisposing)
    {
        if (isDisposing)
        {
            var editText = EditText;
            if (editText != null && _subscribed)
            {
                editText.EditorAction -= HandleEditForChange;
                _subscribed = false;
            }
        }
        base.Dispose(isDisposing);
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM