Xamarin - 输入文本后如何更改输入字段的背景颜色?

问题描述:

我希望能够在输入数据后更改Entry fieldbackground color,因此需要不断检查该字段是否为空,但代码对我无效,因为在应用程序中不起作用并且没有按钮可以工作,“名称”是在的XAML file中设置的名称。Xamarin - 输入文本后如何更改输入字段的背景颜色?

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

请说明这是什么意思,当你说它不为你工作,你得到一个错误的输出?错误? – BlooB

您可以测试内容在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中的属性... –