Xamarin.Forms iOS CursorPosition不兑现 [英] Xamarin.Forms iOS CursorPosition not being honored

查看:80
本文介绍了Xamarin.Forms iOS CursorPosition不兑现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Xamarin.Forms应用程序中,我有一个设置光标位置的行为(该行为充当我们输入字段的掩码).

In my Xamarin.Forms app, I have a behavior that sets the cursor's position (the behavior acts as a mask for our input field).

通过调用targetEntry.CursorPosition = newCursorPosition设置CursorPosition.在Android上,这工作得很好,但是在iOS上,光标总是移到文本的结尾,是iOS版本的特定错误,还是只是全部?

The CursorPosition is set by calling: targetEntry.CursorPosition = newCursorPosition. This works perfectly fine on Android, but on iOS the cursor always goes to the end of the text, is it an iOS version specific bug, or is it just across the board?

与行为有关的所有其他事情(确保正确屏蔽文本,正确删除下划线字符(用作用户输入数据的指南))均应正常工作.如果需要,我可以发布更多代码

Everything else about the behavior (ensuring that the text is properly masked, that it properly removes the underline character which acts as the guide for where a user should input their data) works as it should. I can post more of my code if needed

newCursorPosition是一个静态的int变量

newCursorPosition is a static int variable

包括我的代码

公共类DurationViewMaskBehavior:行为 { 公共静态DurationViewMaskBehavior实例= new DurationViewMaskBehavior(); 私有静态int cursorPosition = 0;

public class DurationViewMaskBehavior : Behavior { public static DurationViewMaskBehavior Instance = new DurationViewMaskBehavior(); private static int cursorPosition = 0;

    protected override void OnAttachedTo(BorderlessEntry entry) {
        entry.TextChanged += OnInput;
        base.OnAttachedTo(entry);
    }

    protected override void OnDetachingFrom(BorderlessEntry entry) {
        entry.TextChanged -= OnInput;
        base.OnDetachingFrom(entry);
    }

    /// <summary>
    /// This method exists to ensure that if the user tried to shape the field to be something it shouldn't
    /// that it will return the mask to the correct format; this is to ensure that we only have valid data and also that 
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    private bool PatternMatchCheck(string text) {

        var regex = @"^(([0-9]\d{1}|__|[0-9]_|[0-9]|[0-9]*__|[0-9]\d{1}_| *) Min, ([0-9]\d{1}|__|[0-9]_|[0-9]|[0-9]__|[0-9]\d{1}_|) Sec|((Min, Sec)|[0-9]+ Min, Sec|Min, [0-9]+ Sec))$";

        var match = Regex.Match(text, regex, RegexOptions.IgnoreCase);

        return match.Success;
    }

    private void OnInput(object sender, TextChangedEventArgs args) {

        //Only fire this behavior when the user actually inputs some type of value
        if(!string.IsNullOrWhiteSpace(args.NewTextValue)) {

            var text = args.NewTextValue;

            //How this works so far:
            //At first you get the input, so text.length == 1 is fine. Then
            //it updates itself, so really for every input this event fires twice. 
            //After that we will have to look at the actual text itself so we can use 
            //the index instead

            var targetObject = ((BorderlessEntry)sender);

            if(!PatternMatchCheck(args.NewTextValue) && args.OldTextValue.Length > 2) {
                targetObject.Text = args.OldTextValue;
                return;
            }

            if (text.Length == 1) {
                targetObject.Text = $"{text}_ Min, __ Sec";
                cursorPosition = 1;
            } 

            if(text.Contains("_")) {
                if (text.Length > 1 && (text[2] != '_' || text[1] != '_')) {
                    if (text[2] == '_' && text[0] != '_') {
                        targetObject.Text = $"{text.Substring(0, 2)} Min, __ Sec";
                        cursorPosition = 8;
                    }
                }

                if (text.Length > 1 && text[8] != '_' && text[9] == '_' && text[8] != ' ') {
                    targetObject.Text = $"{text.Substring(0, 2)} Min, {text[8]}_ Sec";
                    cursorPosition = 9;
                }

                if (text.Length > 1 && text[8] != '_' && text[9] != '_') {
                    targetObject.Text = $"{text.Substring(0, 2)} Min, {text.Substring(8, 2)} Sec";
                    cursorPosition = text.Length-1;
                }
            }


            targetObject.CursorPosition = cursorPosition;
        }
    }
}

推荐答案

弄清楚为什么,看起来iOS需要我绑定另一个处理光标设置的.TextChanged事件.这很奇怪而且很落后,但是可以正常工作.

Figured out why, looks like iOS requires me to bind another .TextChanged event that handles the setting of the cursor. It is strange and backward, but it works.

private void SetCursor(object sender, TextChangedEventArgs args) {
            ((BorderlessEntry)sender).CursorPosition = cursorPosition;
        }

if(isRegistered) {
                    targetObject.TextChanged -= SetCursor;
                    isRegistered = false;
                }
                else {
                    targetObject.TextChanged += SetCursor;
                    isRegistered = true;
                }

这篇关于Xamarin.Forms iOS CursorPosition不兑现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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