在软件键盘上绑定"GO"键 [英] Binding 'GO' key on Software Keyboard

查看:113
本文介绍了在软件键盘上绑定"GO"键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录按钮绑定到ViewModel内部存在的命令,如下所示:

this.AddBindings(new Dictionary<object, string>{{btnLogin,"TouchUpInside LoginCommand"}});

如果在键盘外部或UITextField上进行触摸,则会抑制软件键盘. 为了避免用户必须单击屏幕上的某个位置以使键盘消失然后按登录按钮,我想将ViewModel中定义的登录命令与键盘.这可能吗?

解决方案

简单的解决方案是上一个答案中给出的解决方案.

但是,如果您想走得更远,则可以添加自定义绑定,实现类似以下内容的

public class MvxUITextFieldShouldReturnTargetBinding
    : MvxTargetBinding
{
    private ICommand _command;

    protected UITextField View
    {
        get { return Target as UITextField; }
    }

    public MvxUITextFieldShouldReturnTargetBinding(UITextField target)
        : base(target)
    {
        target.ShouldReturn = HandleShouldReturn;
    }

    private bool HandleShouldReturn(UITextField textField)
    {
        if (_command == null)
            return false;

        var text = textField.Text;
        if (!_command.CanExecute(text))
            return false;

        textField.ResignFirstResponder();
        _command.Execute(text);
        return true;
    }

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

    public override void SetValue(object value)
    {
        var command = value as ICommand;
        _command = command;
    }

    public override System.Type TargetType
    {
        get { return typeof(ICommand); }
    }


    protected override void Dispose(bool isDisposing)
    {
        base.Dispose(isDisposing);
        if (isDisposing)
        {
            var editText = View;
            if (editText != null)
            {
                editText.ShouldReturn = null;
            }
        }
    }
}

您可以在设置过程中将其注册为:

        registry.RegisterCustomBindingFactory<UITextField>("ShouldReturn",
                                                           textField => new MvxUITextFieldShouldReturnTargetBinding(textField));

我认为这会起作用-它应该允许您绑定MvxCommandMvxCommand<string>

有关自定义绑定的更多信息,请参见 http://mvvmcross.wordpress.com 中的N = 28

I've got a log-in button bound to a command that exists inside my ViewModel like so:

this.AddBindings(new Dictionary<object, string>{{btnLogin,"TouchUpInside LoginCommand"}});

The software keyboard is suppressed if a touch is made, somewhere outside the keyboard or UITextField. To prevent the user from having to click once somewhere on the screen to make the keyboard disappear and then pressing the log-in button, I would like to associate the log-in command defined in the ViewModel to the 'GO' key in the keyboard. Is this possible?

解决方案

The simple solution is the one given in the previous answer.

However, if you wanted to go further, then you could add a custom binding, implemented something like:

public class MvxUITextFieldShouldReturnTargetBinding
    : MvxTargetBinding
{
    private ICommand _command;

    protected UITextField View
    {
        get { return Target as UITextField; }
    }

    public MvxUITextFieldShouldReturnTargetBinding(UITextField target)
        : base(target)
    {
        target.ShouldReturn = HandleShouldReturn;
    }

    private bool HandleShouldReturn(UITextField textField)
    {
        if (_command == null)
            return false;

        var text = textField.Text;
        if (!_command.CanExecute(text))
            return false;

        textField.ResignFirstResponder();
        _command.Execute(text);
        return true;
    }

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

    public override void SetValue(object value)
    {
        var command = value as ICommand;
        _command = command;
    }

    public override System.Type TargetType
    {
        get { return typeof(ICommand); }
    }


    protected override void Dispose(bool isDisposing)
    {
        base.Dispose(isDisposing);
        if (isDisposing)
        {
            var editText = View;
            if (editText != null)
            {
                editText.ShouldReturn = null;
            }
        }
    }
}

which you could register during setup as:

        registry.RegisterCustomBindingFactory<UITextField>("ShouldReturn",
                                                           textField => new MvxUITextFieldShouldReturnTargetBinding(textField));

I think that would work - it should allow you to bind a MvxCommand or an MvxCommand<string>

For more on custom bindings, see N=28 in http://mvvmcross.wordpress.com

这篇关于在软件键盘上绑定"GO"键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆