简体   繁体   中英

How to hide the error label in Xamarin forms, when the Entry field is not visible?

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?

在此处输入图片说明

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" />

in your ViewModel

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;
           }
        }) ;

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM