【问题标题】:Cannot refresh listview with observablecollection无法使用 observablecollection 刷新列表视图
【发布时间】:2018-09-23 05:05:18
【问题描述】:

当一个项目被删除时,我试图刷新我的listview,但每次它都会给我这个错误:

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

在更新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 上。

【问题讨论】:

  • ObservableCollection 中删除项目时显示的代码在哪里?
  • 我从后端执行,然后重新加载项目列表。
  • 我将编辑帖子以添加整个内容。
  • @apineda 我已经添加了整个东西
  • @Kay 我尝试了这两个选项,但它们都不起作用。那时我真的很绝望,这就是为什么我分享的代码是一团糟,我只是在尝试一些东西。无论如何感谢您的帮助!

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


【解决方案1】:

更新 iOS 后问题已得到修复。该问题是由于有问题的 iOS 版本索引对象引起的。更新后,一切都像往常一样顺利。如果有人遇到此问题(仅在 iOS 上),请尝试同时更新 iOS 和 Xamarin 表单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 2013-01-15
    相关资源
    最近更新 更多