Visual studio 2015 登录页未在xamarin表单中验证

Visual studio 2015 登录页未在xamarin表单中验证,visual-studio-2015,xamarin.forms,Visual Studio 2015,Xamarin.forms,我使用xamarin表单创建了登录页面,创建了名为validationbehavior.cs的类文件,并通过Behavior类验证了输入字段 xaml登录页面代码: 以下是我的ValidationBehavior.cs代码: 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 使用Xamarin.Forms; 使用System.Text.RegularExpressions;

我使用xamarin表单创建了登录页面,创建了名为validationbehavior.cs的类文件,并通过Behavior类验证了输入字段

xaml登录页面代码:


以下是我的ValidationBehavior.cs代码:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用Xamarin.Forms;
使用System.Text.RegularExpressions;
名称空间登录用户
{    
公共类ValidationBehavior:行为
{    
常量字符串pwRegex=@“^(?=.[A-Za-z])(?=.*\d)(?=.[$@$!%*.#?&])[A-Za-z\d$@$!%*.*.?&]{8,}$”;
受保护的覆盖无效数据到(条目可绑定)
{
bindable.TextChanged+=HandleTextChanged;
碱。可粘合的DTO(可粘合);
}
私有void HandleTextChanged(对象发送方,textchangedventargs e)
{
bool IsValid=false;
IsValid=(Regex.IsMatch(e.NewTextValue,pwRegex));
((输入)sender.TextColor=IsValid?Color.Default:Color.Red;
}
受保护的覆盖无效OnDetachingFrom(条目可绑定)
{                          
bindable.TextChanged-=HandleTextChanged;
基础。从(可装订)开始连接;
}
}
}

因此,当我在android emulator中运行该程序时,登录设计工作正常,但验证不起作用。

也许您应该将IsValid变量更改为如下属性:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用Xamarin.Forms;
使用System.Text.RegularExpressions;
名称空间登录用户
{    
公共类ValidationBehavior:行为
{    
常量字符串pwRegex=@“^(?=.[a-z])(?=.[a-z])(?=.*\d)(?=.[^\da-zA-z])。{8,15}$”;
静态只读BindablePropertyKey IsValidPropertyKey=BindableProperty.CreateReadOnly(“IsValid”、typeof(bool)、typeof(ValidationBehavior)、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,pwRegex,RegexOptions.IgnoreCase,TimeSpan.frommilluses(250));
((输入)sender.TextColor=IsValid?Color.Default:Color.Red;
}
受保护的覆盖无效OnDetachingFrom(条目可绑定)
{                          
bindable.TextChanged-=HandleTextChanged;
}
}
}
另一个错误也是您的正则表达式,请尝试以下操作:

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,15}$
如果您还希望至少需要一个特殊字符(这可能是个好主意),请尝试以下方法:

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,15}$
通过我的示例,您可以得到如下结果:

密码:123456

密码:AbcDe12


如果在HandleTextChanged方法中放置断点,是否会正确调用它?它的回报是什么?