我的登录页面有一个标签,当身份验证失败时,我会显示一条错误消息。当我绘制它时,我已将可见性设置为 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();
}
}
我的按钮在与上面相同的 View 模型类中绑定(bind)到此,如果它没有通过简单例份验证,它会尝试设置属性 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
公开,以便它对其他类可见。
当它仍然不起作用时,将绑定(bind)更改为:
errorMessage.SetBinding(Label.IsVisibleProperty, LoginViewModel.ErrorMessagePropertyName);
关于c# - Xamarin 表单绑定(bind)标签 IsVisibleProperty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33396538/