简体   繁体   English

Xamarin形成绑定标签IsVisibleProperty

[英]Xamarin forms binding Label IsVisibleProperty

My login page has a label, that when authentication fails i'll display an error message. 我的登录页面有一个标签,当验证失败时,我将显示一条错误消息。 When i'm drawing it i have set the Visibility is set to false. 当我绘图时,我已将可见性设置为false。 After i authenticate i want to come back to the ContentPage and set the label to visible. 在我进行身份验证后,我想回到ContentPage并将标签设置为可见。 It just doesn't do anything I've tried setting the BindingMode enum to TwoWay but that enables it straight away and then i cant turn it off 它只是没有做任何我尝试将BindingMode枚举设置为TwoWay的东西,但它立即启用它然后我不能把它关闭

in the loginPage 在loginPage中

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

In the ViewModel page 在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();
    }
}

My button is bound to this in the same view model class as above, If it doens't pass the simple authentication it tries to set property DisplayError 我的按钮在上面相同的视图模型类中绑定到它,如果它没有通过简单的身份验证,它会尝试设置属性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);
}

The OnPropertyChanged method OnPropertyChanged方法

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

Make the property DisplayError public so it becomes visible to other classes. 使属性DisplayError公共,以便其他类可见。 When it still doesn't work change the binding to: 当它仍然无法工作时将绑定更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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