2017-10-12 80 views
1

我希望能夠在輸入數據後更改Entry fieldbackground color,因此需要不斷檢查該字段是否爲空,但代碼對我無效,因爲在應用程序中不起作用並且沒有按鈕可以工作,「名稱」是在的XAML file中設置的名稱。Xamarin - 輸入文本後如何更改輸入字段的背景顏色?

public void BackgroundColourEntry() 
    { 
     while (true) 
     { 
      if (Name.Text != "" && ClientName.Text != null) 
      { 
       Name.BackgroundColor = Color.FromHex("#2c3e50"); 
      } 
     } 
    } 
+0

請說明這是什麼意思,當你說它不爲你工作,你得到一個錯誤的輸出?錯誤? – BlooB

回答

2

您可以測試內容在TextChangedEntry

例子:

BackgroundColourEntry.TextChanged += (sender, e) => 
{ 
    var entry = sender as Entry; 
    if (string.IsNullOrEmpty(entry.Text)) 
    { 
     entry.BackgroundColor = Color.Red; 
    } 
    else 
    { 
     entry.BackgroundColor = Color.Green; 
    } 
}; 
+0

謝謝!有效! – Ciaran

+0

或將其綁定到ViewModel中的屬性... –

相關問題