通过点击文本字段导航

时间:2018-04-15 19:42:48

标签: c# ios xamarin mvvmcross ios11

对于关于火车时间的项目,我想确保当我点击UITextfield时,我会进入一个tableview。从这里我从列表中选择一个电台,然后他必须回到第一个视图,但他必须填写相应文本字段中的电台。

如果有人可以帮助我,这将是非常棒的。

提前感谢。

整个项目在MvvmCross,IOS(Xamarin)中很疯狂

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

我可能会听UITextFieldEditingDidBegin来确定UITextField是焦点。但是,开箱即用不支持此绑定。

您接下来要抓住这个类:https://github.com/MvvmCross/MvvmCross/blob/develop/MvvmCross/Platforms/Ios/Binding/Target/MvxUITextFieldTextFocusTargetBinding.cs这是UITextFieldEditingDidEnd的绑定定义,只需将其更改为UITextFieldEditingDidBegin并触发ICommand当那个事件发生时。

这看起来像是:

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;
    }
}

然后,您需要在Setup中的FillTargetFactories课程中注册:

protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
    base.FillTargetFactories(registry);

    registry.RegisterCustomBindingFactory<UIControl>(
        "EditingDidEnd",
        view =>
            new MvxUITextFieldEditingDidBeginTargetBinding(view));
}

这将允许您绑定到绑定中的EditingDidEnd

set.Bind(textField).For("EditingDidEnd").To(vm => vm.SomeCommand);

您可以在MvvmCross文档中阅读更多有关自定义绑定的内容:https://www.mvvmcross.com/documentation/advanced/custom-data-binding