Xamarin 表单绑定标签 IsVisibleProperty

Xamarin forms binding Label IsVisibleProperty

我的登录页面有一个标签,当身份验证失败时我会显示一条错误消息。当我绘制它时,我已将可见性设置为 false。在我进行身份验证后,我想返回到 ContentPage 并将标签设置为可见。它只是没有做任何事情我已经尝试将 BindingMode 枚举设置为 TwoWay 但这会立即启用它然后我无法将其关闭

在登录页面

Label errorMessage = new Label { IsVisible = false, Text = "Invalid credentials please try again", TextColor = Color.Red };
errorMessage.SetBinding(IsVisibleProperty, LoginViewModel.ErrorMessagePropertyName);

在 ViewModel 页面中

public const string ErrorMessagePropertyName = "DisplayError";
private bool _displayError = false;
private bool DisplayError
{
    get { return _displayError; }
    set
    {
        if (value.Equals(_displayError)) return;

        _displayError = value;
        OnPropertyChanged();
    }
}

我的按钮在与上面相同的视图模型 class 中绑定到此,如果它没有通过简单身份验证,它会尝试设置 属性 DisplayError

protected async Task ExecuteLoginCommand()
{
    string eventMessage= string.Format("Authenticating User:{0} on {1}", UserName, DateTime.UtcNow);
    Logger.LogEvent(eventMessage);

    if(UserName == "g" && Password.Length > 2)
    {
        Application.Current.Properties.Add(Constants.KEY_IS_AUTHENTICATED, true);

        await _navigation.PopAsync();
    }
    else
    {
        DisplayError = true;
        string message = string.Format("Invalid user tried to log into device at this time {0}",DateTime.Now);
        Logger.LogEvent(message);
    }

    Debug.WriteLine(UserName);
    Debug.WriteLine(Password);
}

OnPropertyChanged方法

protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this,
            new PropertyChangedEventArgs(propertyName));
    }
}

使 属性 DisplayError public 对其他 类 可见。 当它仍然不起作用时,将绑定更改为:

 errorMessage.SetBinding(Label.IsVisibleProperty, LoginViewModel.ErrorMessagePropertyName);