【问题标题】:How to disable / cancel undo button in xamarin如何在 xamarin 中禁用/取消撤消按钮
【发布时间】:2017-08-18 22:13:21
【问题描述】:

我正在创建一个具有条目的应用程序。我正在尝试将条目限制为仅允许数字输入。我已经尝试过使用键盘 =“数字”。然而,对于 iPad,键盘上的字符不仅仅是数字。所以我不得不限制输入的内容。但是,当我这样做时,例如,如果用户键入括号,它确实会阻止输入字符。但是如果用户按下撤消,它就会崩溃。我认为这是因为软件键盘与应用程序是分开的,所以它正在寻找那个括号字符,但它不存在。这是我的代码:

private void Entry_TextChanged(object sender, TextChangedEventArgs e)
    {
        Entry theEntry = (Entry)sender;
        string entryText = theEntry.Text;
        if(entryText != null && entryText.Length != 0)
        {
            char theMostRecentInput = entryText[entryText.Length - 1];
            if(!Extension.IsNumeric(theMostRecentInput))
            {
                theEntry.TextChanged -= Entry_TextChanged;
                theEntry.Text = e.OldTextValue;
                theEntry.TextChanged += Entry_TextChanged;
            }
        }
    }

感谢您的帮助!

【问题讨论】:

  • 不确定如何解决此问题,但您还有另一个错误。您假设最近键入的字符是字符串中的最后一个字符。解决此问题的更好方法可能是在用户完成表单时验证数字,并在字符串包含无效字符时通知用户。在用户输入的文本框中设置字符串会导致意外结果。

标签: c# visual-studio xamarin xamarin.ios keyboard


【解决方案1】:

当对输入字段实施特殊字符、最大限制等验证时,将出现此问题。 到那时,撤消操作的字符数将超过 iOS ShouldChangeCharacters Delegate 中当前输入字段文本字符的长度。这会导致应用程序崩溃。 解决方案之一是在这种情况下返回 false 而不是禁用撤消按钮。以下解决方案对我有用。

public class ExtEntryRenderer : EntryRenderer
{
    protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
            if (Control != null)
            {
            Control.ShouldChangeCharacters += (UITextField textField, NSRange range, string replacementString) =>
                {
                if (range.Location + range.Length > ((UITextField)textField).Text.Length)
                        return false;
                    return true;
                };
            }                          
    }
}

【讨论】:

    【解决方案2】:

    我会在Entry 自定义渲染器中执行此操作,这样您就可以通过ShouldChangeCharacters 控制输入,而不必通过允许输入然后必须删除处理程序并将文本更改回旧值...

    这是一个允许数字的简单示例,它还自动处理剪贴板粘贴非数字字符串,因为这些字符串是不允许的。我正在使用NSCharacterSet.DecimalDigits 字符集,因为它会被操作系统国际化,但您可以允许/禁止您选择的任何字符。

    您还可以在不允许/拒绝的条目中加入触觉、视觉或音频反馈...

    [assembly: ExportRenderer(typeof(NumericEntry), typeof(NumericEntryRenderer))]
    namespace Forms_PCL_Tester.iOS
    {
        public class NumericEntryRenderer : EntryRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Entry> e)
            {
                base.OnElementChanged(e);
                if (e.NewElement != e.OldElement)
                    if (Control != null)
                    {
                        Control.KeyboardType = UIKeyboardType.NumbersAndPunctuation;
                        Control.ShouldChangeCharacters += (UITextField textField, NSRange range, string replacementString) =>
                         {
                             foreach (var aChar in replacementString)
                                 if (!NSCharacterSet.DecimalDigits.Contains(aChar))
                                     return false;
                             return true;
                         };
                    }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-04-19
      • 2011-03-29
      • 2016-09-22
      • 1970-01-01
      • 2011-02-24
      • 2016-06-22
      • 2013-05-10
      • 2016-07-09
      • 1970-01-01
      相关资源
      最近更新 更多