I would probably listen to UITextFieldEditingDidBegin
to figure out that the UITextField is in focus. However, this binding is not supported out of the box.
What you would then do is to grab this class: https://github.com/MvvmCross/MvvmCross/blob/develop/MvvmCross/Platforms/Ios/Binding/Target/MvxUITextFieldTextFocusTargetBinding.cs which is the binding definition for UITextFieldEditingDidEnd
and just change it to the UITextFieldEditingDidBegin
and have it trigger a ICommand
when that event fires.
This would look something like:
public class MvxUITextFieldEditingDidBeginTargetBinding : MvxTargetBinding
{
private IDisposable _subscription;
private ICommand _command;
protected UITextField TextField => Target as UITextField;
public override Type TargetType => typeof(ICommand);
public override MvxBindingMode DefaultMode => MvxBindingMode.OneWay;
public MvxUITextFieldEditingDidBeginTargetBinding(object target)
: base(target)
{
}
public override void SetValue(object value)
{
_command = value as ICommand;
}
public override void SubscribeToEvents()
{
var textField = TextField;
if (TextField == null) return;
_subscription = textField.WeakSubscribe(nameof(textField.EditingDidBegin), HandleEditingBegin);
}
private void HandleEditingBegin(object sender, EventArgs e)
{
if (_command == null) return;
if (!_command.CanExecute(null)) return;
_command.Execute(null);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (!isDisposing) return;
_subscription?.Dispose();
_subscription = null;
}
}
Then you would need to register it in your Setup
class in FillTargetFactories
:
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
base.FillTargetFactories(registry);
registry.RegisterCustomBindingFactory<UIControl>(
"EditingDidEnd",
view =>
new MvxUITextFieldEditingDidBeginTargetBinding(view));
}
This will allow you to bind to EditingDidEnd
in a binding:
set.Bind(textField).For("EditingDidEnd").To(vm => vm.SomeCommand);
You can read a bit more about custom bindings in the MvvmCross documentation: https://www.mvvmcross.com/documentation/advanced/custom-data-binding