C# 表单如何根据条件绑定背景色

C# 表单如何根据条件绑定背景色,c#,android,ios,xaml,xamarin,C#,Android,Ios,Xaml,Xamarin,如何根据状态绑定背景色 我试过这个代码,但没用 var cat = JsonConvert.DeserializeObject<List<TableStat>>(response); for(int i = 0;i<cat.Count;i++) { if (cat[i].table_status == "Available")

如何根据状态绑定背景色 我试过这个代码,但没用

var cat = JsonConvert.DeserializeObject<List<TableStat>>(response);
                for(int i = 0;i<cat.Count;i++)
                {
                    if (cat[i].table_status == "Available")
                    {
                        color = "Green";
                        this.BindingContext = color;
                    }
                    else if (cat[i].table_status == "Unavailable")
                    {
                        color = "Black";
                        this.BindingContext = color;
                    }

                }
var cat=JsonConvert.DeserializeObject(响应);

对于(int i=0;i您正在更改this.BindingContext,而不调用观察者。因此颜色会更改,但视图不会收到通知

将“set”添加到包含RaisePropertyChanged的颜色,如下所示:

 set { color = value; 
         RaisePropertyChanged("Model");  //<- this should tell the view to update
     }
设置{color=value;

RaisePropertyChanged(“Model”);//首先,您只能绑定到公共属性

public Color BGColor { get; set; }

BindingContext = this;
然后在代码中设置该属性的值——您可能还需要在类上实现INPC

            for(int i = 0;i<cat.Count;i++)
            {
                if (cat[i].table_status == "Available")
                {
                    BGColor = Color.Green;
                }
                else if (cat[i].table_status == "Unavailable")
                {
                    BGColor = Color.Black;
                }

            }

for(int i=0;i您是否使用mvvm?是的,我正在使用restapi收集数据@arvindraja您需要创建一个转换器来设置颜色依赖条件,您可以按照此操作,解决方案在哪里?:应该在`InitializeComponent();`BackgroundColor='下面的哪里输入
BindingContext=this
{Binding BGColor}{那么我将像这样绑定BGColor?您也在类上实现了INPC吗?或者,您可以显式地设置BackgroundColor属性而不是使用Bindingswhat's INPC?我如何实现它?INotifyPropertyChanged-这已经在前面的问题中向您解释过了-
            for(int i = 0;i<cat.Count;i++)
            {
                if (cat[i].table_status == "Available")
                {
                    BGColor = Color.Green;
                }
                else if (cat[i].table_status == "Unavailable")
                {
                    BGColor = Color.Black;
                }

            }