当 Entry 字段不可见时,如何在 Xamarin 表单中隐藏错误标签? [英] How to hide the error label in Xamarin forms, when the Entry field is not visible?
问题描述
我正在尝试创建一个有验证错误的登录页面.现在,如果 Entry 字段不可见,也会出现验证错误.当输入字段不可见时,如何隐藏错误标签?如下图: PIN 输入字段在登录页面上不可见,但错误信息:Pin is required,突出显示.请问有人可以提出解决方法吗?
I am trying to create a Login page that has validation errors. Right now the validation errors are also appearing if the Entry field is not visible. How would I hide the error labels, when the entry field is not visible? As shown below: The PIN entry field is invisible on the login page but the error message: Pin is required, highlights. Please could anyone suggest a workaround?
推荐答案
同意 Jason 的意见.您可以使用数据绑定将标签的 IsVisible
绑定到视图模型中的属性.
Agree with Jason . You can use the data-binding to binding the IsVisible
of labelto the property in your viewmodel .
<Label Text="Pin is requored!" TextColor="Red" HorizontalTextAlignment="Center" IsVisible="{Binding isVisible}"/>
<Button Text="sign in" BackgroundColor="Red" TextColor="White" Command="{Binding ClickCommand}" WidthRequest="200" />
在您的视图模型中
public class YourViewModel: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public ICammand ClickCommand {get; set;}
private bool isvisible;
public bool isVisible
{
get
{
return isvisible;
}
set
{
if (isvisible!= value)
{
isvisible= value;
NotifyPropertyChanged();
}
}
public YourViewModel()
{
//...
isVisible = true; //show the label in default
ClickCommand = new Command(() =>
{
if(xxx)
{
isVisible =false;
}
else
{
isVisible =true;
}
}) ;
}
}
这篇关于当 Entry 字段不可见时,如何在 Xamarin 表单中隐藏错误标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!