如何通过Web服务更改单元格背景?

时间:2017-11-01 19:44:08

标签: asynchronous xamarin xamarin.forms json.net syncfusion

我正在使用Syncfusion的SfCalendar for Xamarin,我试图在每天显示的基于服务器响应的真/假条件。如果我在请愿之前更改了单元格背景颜色,它会没有问题,但是如果我等待它的响应,即使我在主线程上它也不会改变。

    public async Task CalDates(object sender, Syncfusion.SfCalendar.XForms.MonthCell args)
    {
        HttpClient hTTPClient = new HttpClient();
        var client = new HttpClient();
        client.BaseAddress = new Uri(Utils.Utils.baseUrl);
        Dictionary<string, string> dataToSend = new Dictionary<string, string>();
        dataToSend.Add("session", Utils.Utils.session);
        dataToSend.Add("year", args.Date.Year.ToString());
        dataToSend.Add("month", args.Date.Month.ToString());
        dataToSend.Add("day", args.Date.Day.ToString());
        string jsonData = JsonConvert.SerializeObject(dataToSend, new KeyValuePairConverter());
        Debug.WriteLine(jsonData);
        var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
        //If I change backgrounds here, it will work
        HttpResponseMessage response = await client.PostAsync("/getPlan", content);
        //If I do it here, it won't work anymore
        var result = await response.Content.ReadAsStringAsync();
        var resultJson = JObject.Parse(result);
        if ((int)resultJson["status"] == 1)
        {
            Debug.WriteLine(resultJson["data"]);
            if((bool)resultJson["data"] == true){
                try{
                    Device.BeginInvokeOnMainThread(() => {

                        args.BackgroundColor = Color.FromHex("#e8de71");
                        args.TextColor = Color.Gray;
                    });
                }catch(Exception ex){
                    Debug.WriteLine("Error: {0}", ex);
                }
            }
        }
    }

如果我在BeginInvokeOnMainThread中执行其他操作,例如更改标签文本或不使用日历的内容,它将起作用。请帮助我,我已经坚持了一段时间了,而且我的想法已经用完了。

0 个答案:

没有答案