Xamarin形成密码并确认密码验证
我有一个Xamarin形成密码并确认密码验证,xamarin,xamarin.forms,xamarin-studio,Xamarin,Xamarin.forms,Xamarin Studio,我有一个Xamarin Forms应用程序,其中我有一个注册表,其中我需要验证密码并确认密码字段应该相同 是否有任何方法可以使用Xamarin行为实现此功能 我已经为必填字段实施了Xamarin行为验证和电子邮件Regex验证,如下所示- public class RequiredValidatorBehavior : Behavior<Entry> { static readonly BindablePropertyKey IsValidPropertyKe
Xamarin Forms
应用程序,其中我有一个注册表,其中我需要验证密码并确认密码字段应该相同
是否有任何方法可以使用Xamarin行为实现此功能
我已经为必填字段实施了Xamarin行为
验证和电子邮件Regex
验证,如下所示-
public class RequiredValidatorBehavior : Behavior<Entry>
{
static readonly BindablePropertyKey IsValidPropertyKey = BindableProperty.CreateReadOnly("IsValid", typeof(bool), typeof(RequiredValidatorBehavior), false);
static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty;
public bool IsValid
{
get { return (bool)base.GetValue(IsValidProperty); }
private set { base.SetValue(IsValidPropertyKey, value); }
}
protected override void OnAttachedTo(Entry bindable)
{
bindable.Unfocused += HandleFocusChanged;
base.OnAttachedTo(bindable);
}
protected override void OnDetachingFrom(Entry bindable)
{
bindable.Unfocused -= HandleFocusChanged;
base.OnDetachingFrom(bindable);
}
void HandleFocusChanged(object sender, FocusEventArgs e)
{
IsValid = !string.IsNullOrEmpty (((Entry)sender).Text);
}
}
问题是-我在Xamarin
开发方面是新手,不知道如何在behavior
中获取Password
和Confirm Password
字段的值,以便比较它们。我不希望在提交表单时单击按钮进行比较,应在用户键入字段时比较字段。任何代码、帮助或指导都是值得欣赏的。
这对你有帮助。
您将为此使用比较验证器行为,如下所示
<behaviors:CompareValidator x:Name="ComparePasswordsValidator"
CompareToEntry="{Binding Source={x:Reference PasswordEntry}}" />
最后,解决方案:
XAML-
行为-
public class ConfirmPasswordBehavior : Behavior<Entry>
{
static readonly BindablePropertyKey IsValidPropertyKey = BindableProperty.CreateReadOnly("IsValid", typeof(bool), typeof(ConfirmPasswordBehavior), false);
public static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty;
public static readonly BindableProperty CompareToEntryProperty = BindableProperty.Create("CompareToEntry", typeof(Entry), typeof(ConfirmPasswordBehavior), null);
public Entry CompareToEntry
{
get { return (Entry)base.GetValue(CompareToEntryProperty); }
set { base.SetValue(CompareToEntryProperty, value); }
}
public bool IsValid
{
get { return (bool)base.GetValue(IsValidProperty); }
private set { base.SetValue(IsValidPropertyKey, value); }
}
protected override void OnAttachedTo(Entry bindable)
{
bindable.TextChanged += HandleTextChanged;
base.OnAttachedTo(bindable);
}
protected override void OnDetachingFrom(Entry bindable)
{
bindable.TextChanged -= HandleTextChanged;
base.OnDetachingFrom(bindable);
}
void HandleTextChanged(object sender, TextChangedEventArgs e)
{
var password = CompareToEntry.Text;
var confirmPassword = e.NewTextValue;
IsValid = password.Equals (confirmPassword);
}
}
公共类ConfirmPasswordBehavior:Behavior
{
静态只读BindablePropertyKey IsValidPropertyKey=BindableProperty.CreateReadOnly(“IsValid”,typeof(bool),typeof(ConfirmPasswordBehavior),false);
公共静态只读BindableProperty IsValidProperty=IsValidPropertyKey.BindableProperty;
公共静态只读BindableProperty CompareTentryProperty=BindableProperty.Create(“CompareToEntry”、typeof(Entry)、typeof(ConfirmPasswordBehavior)、null);
公共条目比较条目
{
获取{return(Entry)base.GetValue(compareTentryProperty);}
set{base.SetValue(compareTentryProperty,value);}
}
公共布尔是有效的
{
获取{return(bool)base.GetValue(IsValidProperty);}
私有集{base.SetValue(IsValidPropertyKey,value);}
}
受保护的覆盖无效数据到(条目可绑定)
{
bindable.TextChanged+=HandleTextChanged;
碱。可粘合的DTO(可粘合);
}
受保护的覆盖无效OnDetachingFrom(条目可绑定)
{
bindable.TextChanged-=HandleTextChanged;
基础。从(可装订)开始连接;
}
void HandleTextChanged(对象发送者,textchangedventargs e)
{
var password=CompareToEntry.Text;
var confirmPassword=e.NewTextValue;
IsValid=password.Equals(confirmPassword);
}
}
我对Punnet的答案做了一些更改:
首先,修复HandleTextChanged上的null异常,该异常在可比较字符串为null时抛出
void HandleTextChanged(object sender, TextChangedEventArgs e)
{
string theBase = CompareToEntry.Text;
string confirmation = e.NewTextValue;
// here is the change
IsValid = (bool)theBase?.Equals(confirmation);
((Entry)sender).TextColor = IsValid ? Color.Green : Color.Red;
}
第二个是检查验证变为真后可比字符串是否发生了更改
void baseValue_changed(object sender, TextChangedEventArgs e)
{
IsValid = (bool)((Entry)sender).Text?.Equals(thisEntry.Text);
thisEntry.TextColor = IsValid ? Color.Green : Color.Red;
}
因此,新代码成为
public class ComparisonBehavior : Behavior<Entry>
{
private Entry thisEntry;
static readonly BindablePropertyKey IsValidPropertyKey = BindableProperty.CreateReadOnly("IsValid", typeof(bool), typeof(ComparisonBehavior), false);
public static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty;
public static readonly BindableProperty CompareToEntryProperty = BindableProperty.Create("CompareToEntry", typeof(Entry), typeof(ComparisonBehavior), null);
public Entry CompareToEntry
{
get { return (Entry)base.GetValue(CompareToEntryProperty); }
set
{
base.SetValue(CompareToEntryProperty, value);
if (CompareToEntry != null)
CompareToEntry.TextChanged -= baseValue_changed;
value.TextChanged += baseValue_changed;
}
}
void baseValue_changed(object sender, TextChangedEventArgs e)
{
IsValid = ((Entry)sender).Text.Equals(thisEntry.Text);
thisEntry.TextColor = IsValid ? Color.Green : Color.Red;
}
public bool IsValid
{
get { return (bool)base.GetValue(IsValidProperty); }
private set { base.SetValue(IsValidPropertyKey, value); }
}
protected override void OnAttachedTo(Entry bindable)
{
thisEntry = bindable;
if (CompareToEntry != null)
CompareToEntry.TextChanged += baseValue_changed;
bindable.TextChanged += HandleTextChanged;
base.OnAttachedTo(bindable);
}
protected override void OnDetachingFrom(Entry bindable)
{
bindable.TextChanged -= HandleTextChanged;
if (CompareToEntry != null)
CompareToEntry.TextChanged -= baseValue_changed;
base.OnDetachingFrom(bindable);
}
void HandleTextChanged(object sender, TextChangedEventArgs e)
{
string theBase = CompareToEntry.Text;
string confirmation = e.NewTextValue;
IsValid = (bool)theBase?.Equals(confirmation);
((Entry)sender).TextColor = IsValid ? Color.Green : Color.Red;
}
}
公共类比较行为:行为
{
私人入户;
静态只读BindablePropertyKey IsValidPropertyKey=BindableProperty.CreateReadOnly(“IsValid”,typeof(bool),typeof(ComparisonBehavior),false);
公共静态只读BindableProperty IsValidProperty=IsValidPropertyKey.BindableProperty;
公共静态只读BindableProperty CompareTentryProperty=BindableProperty.Create(“CompareToEntry”、typeof(Entry)、typeof(ComparisonBehavior)、null);
公共条目比较条目
{
获取{return(Entry)base.GetValue(compareTentryProperty);}
设置
{
base.SetValue(compareTentryProperty,value);
if(CompareToEntry!=null)
CompareToEntry.TextChanged-=baseValue\u changed;
value.TextChanged+=baseValue\u changed;
}
}
void baseValue_已更改(对象发送方,textchangedventargs e)
{
IsValid=((条目)sender.Text.Equals(thisEntry.Text);
thisEntry.TextColor=IsValid?颜色。绿色:颜色。红色;
}
公共布尔是有效的
{
获取{return(bool)base.GetValue(IsValidProperty);}
私有集{base.SetValue(IsValidPropertyKey,value);}
}
受保护的覆盖无效数据到(条目可绑定)
{
此条目=可绑定;
if(CompareToEntry!=null)
CompareToEntry.TextChanged+=baseValue\u已更改;
bindable.TextChanged+=HandleTextChanged;
碱。可粘合的DTO(可粘合);
}
受保护的覆盖无效OnDetachingFrom(条目可绑定)
{
bindable.TextChanged-=HandleTextChanged;
if(CompareToEntry!=null)
CompareToEntry.TextChanged-=baseValue\u changed;
基础。从(可装订)开始连接;
}
void HandleTextChanged(对象发送者,textchangedventargs e)
{
string theBase=CompareToEntry.Text;
字符串确认=e.NewTextValue;
IsValid=(bool)基?.Equals(确认);
((输入)sender.TextColor=IsValid?Color.Green:Color.Red;
}
}
我开发了自定义控件,以使用数据注释方法验证我们的模型
这里是项目url
它不适用于Windows 8.1手机
下面是项目描述的youtube url,以及如何更有效地使用此控件
如果由于System.InvalidOperationException的原因而要添加异常处理,则在键入密码之前先键入确认密码时会显示异常错误
void HandleTextChanged(object sender, TextChangedEventArgs e)
{
try
{
string theBase = CompareToEntry.Text;
string confirmation = e.NewTextValue;
IsValid = (bool)theBase?.Equals(confirmation);
((Entry)sender).TextColor = IsValid ? Color.FromHex("#e6a94d")/*correct*/ : Color.FromHex("#CD5C5C")/*incorrect*/;
}
catch (System.InvalidOperationException)
{
string theBase = CompareToEntry.Text;
string confirmation = e.NewTextValue;
((Entry)sender).TextColor = IsValid ? Color.FromHex("#e6a94d")/*correct*/ : Color.FromHex("#CD5C5C")/*incorrect*/;
}
}
任何有任何输入的人??感谢您的建议,我终于能够将条目
控件的引用传递到行为
中,然后能够验证值。我已经用我提出的整个解决方案更新了你的答案。再次感谢:)谢谢@Puneet和Manik Arora这很有帮助。我要做的唯一额外的事情就是在IsValid为false时实际“更改”某些内容。在HandleTextChangedMethod中,如果您希望删除不匹配的文本,只需在底部添加此项
public class ComparisonBehavior : Behavior<Entry>
{
private Entry thisEntry;
static readonly BindablePropertyKey IsValidPropertyKey = BindableProperty.CreateReadOnly("IsValid", typeof(bool), typeof(ComparisonBehavior), false);
public static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty;
public static readonly BindableProperty CompareToEntryProperty = BindableProperty.Create("CompareToEntry", typeof(Entry), typeof(ComparisonBehavior), null);
public Entry CompareToEntry
{
get { return (Entry)base.GetValue(CompareToEntryProperty); }
set
{
base.SetValue(CompareToEntryProperty, value);
if (CompareToEntry != null)
CompareToEntry.TextChanged -= baseValue_changed;
value.TextChanged += baseValue_changed;
}
}
void baseValue_changed(object sender, TextChangedEventArgs e)
{
IsValid = ((Entry)sender).Text.Equals(thisEntry.Text);
thisEntry.TextColor = IsValid ? Color.Green : Color.Red;
}
public bool IsValid
{
get { return (bool)base.GetValue(IsValidProperty); }
private set { base.SetValue(IsValidPropertyKey, value); }
}
protected override void OnAttachedTo(Entry bindable)
{
thisEntry = bindable;
if (CompareToEntry != null)
CompareToEntry.TextChanged += baseValue_changed;
bindable.TextChanged += HandleTextChanged;
base.OnAttachedTo(bindable);
}
protected override void OnDetachingFrom(Entry bindable)
{
bindable.TextChanged -= HandleTextChanged;
if (CompareToEntry != null)
CompareToEntry.TextChanged -= baseValue_changed;
base.OnDetachingFrom(bindable);
}
void HandleTextChanged(object sender, TextChangedEventArgs e)
{
string theBase = CompareToEntry.Text;
string confirmation = e.NewTextValue;
IsValid = (bool)theBase?.Equals(confirmation);
((Entry)sender).TextColor = IsValid ? Color.Green : Color.Red;
}
}
void HandleTextChanged(object sender, TextChangedEventArgs e)
{
try
{
string theBase = CompareToEntry.Text;
string confirmation = e.NewTextValue;
IsValid = (bool)theBase?.Equals(confirmation);
((Entry)sender).TextColor = IsValid ? Color.FromHex("#e6a94d")/*correct*/ : Color.FromHex("#CD5C5C")/*incorrect*/;
}
catch (System.InvalidOperationException)
{
string theBase = CompareToEntry.Text;
string confirmation = e.NewTextValue;
((Entry)sender).TextColor = IsValid ? Color.FromHex("#e6a94d")/*correct*/ : Color.FromHex("#CD5C5C")/*incorrect*/;
}
}