简体   繁体   English

Xamarin.Forms iOS CursorPosition不兑现

[英]Xamarin.Forms iOS CursorPosition not being honored

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). 在我的Xamarin.Forms应用程序中,我有一个行为来设置光标的位置(行为充当我们输入字段的掩码)。

The CursorPosition is set by calling: targetEntry.CursorPosition = newCursorPosition . 通过调用以下命令来设置CursorPosition: 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? 这在Android上可以很好地工作,但是在iOS上,光标总是移到文本的结尾,是iOS版本的特定错误,还是只是全部?

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 如果需要,我可以发布更多代码

Edit: newCursorPosition is a static int variable 编辑:newCursorPosition是一个静态的int变量

Edit 2: Including my code 编辑2:包括我的代码

public class DurationViewMaskBehavior : Behavior { public static DurationViewMaskBehavior Instance = new DurationViewMaskBehavior(); 公共类DurationViewMaskBehavior:行为{公共静态DurationViewMaskBehavior实例= new DurationViewMaskBehavior(); private static int cursorPosition = 0; 私有静态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;
        }
    }
}

Figured out why, looks like iOS requires me to bind another .TextChanged event that handles the setting of the cursor. 弄清楚为什么,看起来iOS需要我绑定另一个处理光标设置的.TextChanged事件。 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;
                }

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

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