简体   繁体   English

通过点击文本字段进行导航

[英]Navigation by tap on textfield

For a project about train times, I would like to make sure that when I tap on UITextfield, I go to a tableview. 对于有关火车时间的项目,我想确保当我点击UITextfield时,我进入了一个表格视图。 From this I select a station from the list and he would then have to go back to the first view but he has to fill in the station in the corresponding textfield. 我从列表中选择一个站点,然后他将不得不返回到第一个视图,但是他必须在相应的文本字段中填写该站点。

If someone could help me with this, this would be fantastic. 如果有人可以帮助我,那就太好了。

thanks in advance. 提前致谢。

The whole project is mad in MvvmCross, IOS (Xamarin) 整个项目在MvvmCross,IOS(Xamarin)中发疯

在此处输入图片说明

在此处输入图片说明

I would probably listen to UITextFieldEditingDidBegin to figure out that the UITextField is in focus. 我可能会听UITextFieldEditingDidBegin来弄清楚UITextField是焦点。 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. 然后,您要做的就是获取此类: https : //github.com/MvvmCross/MvvmCross/blob/develop/MvvmCross/Platforms/Ios/Binding/Target/MvxUITextFieldTextFocusTargetBinding.cs ,它是UITextFieldEditingDidEnd的绑定定义,只需更改将其发送到UITextFieldEditingDidBegin并在事件触发时触发ICommand

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 : 然后,您需要在FillTargetFactoriesSetup类中注册它:

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: 这将允许您在绑定中绑定到EditingDidEnd

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 您可以在MvvmCross文档中了解有关自定义绑定的更多信息: https ://www.mvvmcross.com/documentation/advanced/custom-data-binding

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

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