当输入字段不可见时,如何隐藏 Xamarin 表单中的错误标签?

我正在尝试创建一个存在验证错误的登录页面。现在,如果输入字段不可见,也会出现验证错误。当输入字段不可见时,如何隐藏错误标签?如下图: PIN 输入字段在登录页面上不可见,但错误消息:Pin is required,突出显示。请问有人可以建议一个解决方法吗?


https://img3.mukewang.com/65055e39000186cf03780412.jpg

qq_花开花谢_0
浏览 45回答 1
1回答

米琪卡哇伊

您可以使用数据绑定将IsVisibleof 标签绑定到视图模型中的属性。<Label Text="Pin is required!"  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;           }        }) ;    }}
打开App,查看更多内容
随时随地看视频慕课网APP