GO key on soft keyboard

I have a login button associated with a command that exists inside my ViewModel , for example:

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

The UITextField keyboard is suppressed when pressed somewhere outside the keyboard or UITextField . So that the user cannot click once on the screen so that the keyboard disappears, and then click the login button, I would like to associate the login command defined in ViewModel with the “GO” key in the keyboard. Is it possible?

+1
source share
2 answers

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

However, if you want to go further, then you can add a custom binding by implementing 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 installation:

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

I think this will work - it should allow you to bind MvxCommand or MvxCommand<string>

For more information on user bindings, see N = 28 at http://mvvmcross.wordpress.com

+6
source

You cannot bind a button yourself. It depends on the focused UIView that launches the keyboard with the GO button. That is, if it is a UITextField , you can assign a function or delegate the ShouldReturn property, which then runs your LoginCommand . Sort of:

 myUiTextField.ShouldReturn += delegate { ViewModel.LoginCommand.Execute(null); return true; }; 

Read more about properties in the Xamarin.iOS API documentation .

+4
source

All Articles