簡體   English   中英

Xamarin形成綁定標簽IsVisibleProperty

[英]Xamarin forms binding Label IsVisibleProperty

我的登錄頁面有一個標簽,當驗證失敗時,我將顯示一條錯誤消息。 當我繪圖時,我已將可見性設置為false。 在我進行身份驗證后,我想回到ContentPage並將標簽設置為可見。 它只是沒有做任何我嘗試將BindingMode枚舉設置為TwoWay的東西,但它立即啟用它然后我不能把它關閉

在loginPage中

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

我的按鈕在上面相同的視圖模型類中綁定到它,如果它沒有通過簡單的身份驗證,它會嘗試設置屬性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公共,以便其他類可見。 當它仍然無法工作時將綁定更改為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM