C# xamarin表单行为:只允许条目中的字符串,而不允许数字

C# xamarin表单行为:只允许条目中的字符串,而不允许数字,c#,xamarin.forms,C#,Xamarin.forms,我使用下面的代码来禁止用户在接受数字的条目中键入字符串。对于一个只接受字符串而不接受数字的条目,我该如何反转它呢 private static void OnEntryTextChanged(object sender, TextChangedEventArgs args) { if(!string.IsNullOrWhiteSpace(args.NewTextValue)) { bool isV

我使用下面的代码来禁止用户在接受数字的条目中键入字符串。对于一个只接受字符串而不接受数字的条目,我该如何反转它呢

            private static void OnEntryTextChanged(object sender, TextChangedEventArgs args) {

                if(!string.IsNullOrWhiteSpace(args.NewTextValue)) { 
                    bool isValid = args.NewTextValue.ToCharArray().All(IsDigit); //Make sure all characters are numbers

                    ((Entry)sender).Text = isValid ? args.NewTextValue : args.NewTextValue.Remove(args.NewTextValue.Length - 1);
                }
            }

            /// <summary>
            /// Check to see if a character is a digit.
            /// </summary>
            /// <param name="c">The character to check.</param>
            /// <returns><c>true</c> if the character is between <c>0</c> and <c>9</c>.</returns>
            private static bool IsDigit(char c) {
                if(c >= 48) {
                    return c <= 57;
                }

                return false;
            }
private static void OnEntryTextChanged(对象发送方,textchangedventargs args){
如果(!string.IsNullOrWhiteSpace(args.NewTextValue)){
bool isValid=args.NewTextValue.ToCharArray().All(IsDigit);//确保所有字符都是数字
((条目)sender.Text=isValid?args.NewTextValue:args.NewTextValue.Remove(args.NewTextValue.Length-1);
}
}
/// 
///检查字符是否为数字。
/// 
///要检查的字符。
///如果字符介于0和9之间,则为true。
专用静态布尔IsDigit(字符c){
如果(c>=48){

return c应该使用正则表达式。此正则表达式只允许字母:

^[a-zA-Z]+$
现在,在您的方法中,您应该这样写:

const string numberRegex=@“^[a-zA-Z]+$”;
私有静态void OnEntryTextChanged(对象发送方,textchangedventargs args)
{
如果(!string.IsNullOrWhiteSpace(args.NewTextValue))
{ 
IsValid=(Regex.IsMatch(args.NewTextValue,numberRegex,RegexOptions.IgnoreCase,TimeSpan.frommilluses(250));
((条目)sender.Text=IsValid?args.NewTextValue:args.NewTextValue.Remove(args.NewTextValue.Length-1);
}
}
我不知道你的
行为是否正确,但你可以这样创建它:

公共类EntryValidator行为:行为
{
常量字符串numberRegex=@“^[a-zA-Z]+$”;
静态只读BindablePropertyKey IsValidPropertyKey=BindableProperty.CreateReadOnly(“IsValid”,typeof(bool),typeof(EmailValidatorBehavior),false);
公共静态只读BindableProperty IsValidProperty=IsValidPropertyKey.BindableProperty;
公共布尔是有效的
{
获取{return(bool)base.GetValue(IsValidProperty);}
私有集{base.SetValue(IsValidPropertyKey,value);}
}
受保护的覆盖无效数据到(条目可绑定)
{
bindable.TextChanged+=HandleTextChanged;
}
void HandleTextChanged(对象发送者,textchangedventargs e)
{
IsValid=(Regex.IsMatch(e.NewTextValue,numberRegex,RegexOptions.IgnoreCase,TimeSpan.frommilluses(250));
((条目)sender).Text=IsValid?e.NewTextValue:e.NewTextValue.Remove(e.NewTextValue.Length-1);
}
受保护的覆盖无效OnDetachingFrom(条目可绑定)
{
bindable.TextChanged-=HandleTextChanged;
}
}
在您的视图(xaml文件)中,如下所示:



替换对bool isValid=args.NewTextValue.ToCharArray().All(IsDigit)的调用;对args.NewTextValue.ToCharArray()ToCharArray().All(char.isleter)的调用;仅供参考。如果光标位置不在末尾,此代码将无法验证-它将只删除最后一个字符,而不是新字符