Xamarin.Forms iOS CursorPosition不兑现

时间:2019-05-24 17:50:31

标签: c# xamarin.forms

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

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

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

编辑:newCursorPosition是一个静态的int变量

编辑2:包括我的代码

公共类DurationViewMaskBehavior:行为     {         公共静态DurationViewMaskBehavior实例= new DurationViewMaskBehavior();         私有静态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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

找出原因,看来iOS要求我绑定另一个处理光标设置的.TextChanged事件。这很奇怪而且很落后,但是行得通。

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

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