无法使用observablecollection刷新列表视图

时间:2018-04-12 22:33:25

标签: ios xamarin xamarin.forms xamarin.ios xamarin.android

我正在尝试从项目中删除项目时刷新listview,但每次它都会给我这个错误:

  

System.ArgumentOutOfRangeException:指定的参数超出了有效值的范围。   参数名称:index。

在更新ObservableCollection之前,我这样做:

    Groups = new ObservableCollection<RequestGroups>();

然后我填写:

    var temp = (JArray)resultJson["data"];
            JArray jarr = temp;
            foreach (JObject contents in jarr.Children<JObject>())
            {
                Requests obj = new Requests();
                obj.Id = (int)contents["id"];
                Client c = new Client();
                c.address = contents["address"].ToString();
                c.phone = contents["phone"].ToString();
                c.name = contents["user"].ToString();
                obj.Client = c;
                obj.Date = contents["date"].ToString();
                obj.Duration = contents["duration"].ToString();
                obj.DurationText = "Duración: "+contents["duration"].ToString()+"h";
                obj.Price = "$" + contents["price"].ToString();
                String[] cDate = obj.Date.Split(' ');
                String cHour = cDate[1]+" "+cDate[2];
                obj.Hour = cHour;
                String[] date = cDate[0].Split('-');
                String title = months[date[1]] + " " + date[0];
                obj.Title = title;
                bool flag = false;
                foreach(RequestGroups rqG in Groups){
                    if(rqG.Title.Equals(title)){
                        rqG.Add(obj);
                        flag = true;
                    }
                }
                if(!flag){
                    RequestGroups rq = new RequestGroups(title, date[1] + "-" + date[0]);
                    rq.Add(obj);
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        Groups.Add(rq);
                    });
                }

            }

这是删除项目的地方:

    private async Task UpdateRequest(int status,int idEvent)
    {
        HttpClient hTTPClient = new HttpClient();
        var client = new HttpClient();
        client.Timeout = TimeSpan.FromSeconds(60);
        client.BaseAddress = new Uri(Utils.baseUrl);

        Dictionary<string, string> dataToSend = new Dictionary<string, string>();
        dataToSend.Add("session", Utils.loginKey);
        dataToSend.Add("eventId", idEvent+"");
        dataToSend.Add("status", status.ToString());
        string jsonData = Newtonsoft.Json.JsonConvert.SerializeObject(dataToSend, new KeyValuePairConverter());

        var contentVar = new StringContent(jsonData, Encoding.UTF8, "application/json");
        try
        {
            HttpResponseMessage response = await client.PostAsync("/UpdateEvent", contentVar);
            var result = await response.Content.ReadAsStringAsync();
            if (response.IsSuccessStatusCode)
            {
                string contents = await response.Content.ReadAsStringAsync();
                var resultJson = JObject.Parse(result);
                if ((int)resultJson["status"] == 0)
                {
                    await base.DisplayAlert((string)resultJson["msg"], "", "OK");
                    return;
                }
                else if ((int)resultJson["status"] == 1)
                {
                    //I'm currently trying to reload the whole view, before this was calling the method above.
                    await this.mainPage.Navigation.PushAsync(new NavigationPage(new MasterMenu.MainPage()));
                    await getRequests();
                }
                else
                {
                    await base.DisplayAlert("Error procesando la solicitud, intente más tarde", "", "Ok");
                    return;
                }
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Error update request: {0}", ex);
        }
        await Task.Delay(1);
    }

如果我这样离开,UI将不会更新。请帮助我,因为我已经在这个问题上苦苦挣扎了2天了。它完全发生在iOS上。

1 个答案:

答案 0 :(得分:1)

更新iOS后问题已修复。问题是由于有一个错误的iOS版本导致索引对象的问题。更新后,一切都像往常一样顺利。如果有人遇到此问题(仅限iOS),请尝试更新iOS和Xamarin表单。