当我调用相同的URL时如何更新ObservableCollection?

时间:2014-05-11 16:57:25

标签: c# xaml windows-phone-7 windows-phone-8

我有问题,当我在Windows手机中调用相同的网址时,我不知道如何更新ObservableCollection。

问题是: - 我第一次调用URL然后将其添加到我的列表框中,然后在一分钟后我调用相同的URL并且结果有新数据(添加新数据或删除旧数据),我不知道如何搜索用于检查所有新数据是否存在的旧数据,如果不存在,则如何添加未收到的新行而不进行复制。

请告诉我:(

这是我如何调用URL以及如何添加数据: -

 private void GetOpentPos (Object sender, EventArgs e)
    {


        var request = HttpWebRequest.Create(new Uri("http://74.54.46.178/vertexweb10/webservice.svc/GetOpenPositions?AccountId=1122336675")) as HttpWebRequest;

        request.Method = "GET";

        if (request.Headers == null)
        {
            request.Headers = new WebHeaderCollection();
        }
        request.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString();
        request.CookieContainer = cookieJar2;
        request.BeginGetResponse(ar =>
        {
            HttpWebRequest req2 = (HttpWebRequest)ar.AsyncState;
            using (var response = (HttpWebResponse)req2.EndGetResponse(ar))
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (var reader = new StreamReader(stream))
                    {
                        var outerRoot4 = JsonConvert.DeserializeObject<OuterRootObject4>(reader.ReadToEnd());
                        JArray jsonArray = JArray.Parse(outerRoot4.d);
                        JToken jsonArray_Item = jsonArray.First;

                        DispatchInvoke(() =>
                        {


                            while (jsonArray_Item != null)
                            {


                                string SymbolNameTra = jsonArray_Item.Value<string>("SymbolName");
                                string TypeTra = jsonArray_Item.Value<string>("BuySell");
                                double AmountTra = jsonArray_Item.Value<double>("Amount");
                                double ProfitLossTra = jsonArray_Item.Value<double>("ProfitLoss");
                                int PosID = jsonArray_Item.Value<int>("ID");



                                DataReceivedCollectionTr.Add(new DataTr() { SymbolNameTr = SymbolNameTra, TypeTr = TypeTra, AmountTr = AmountTra, ProfitLossTr = ProfitLossTra,PosID = PosID });

                                jsonArray_Item = jsonArray_Item.Next;
                            }

                        }
);


                    }
                }

            }

        }, request);



    }

1 个答案:

答案 0 :(得分:1)

按一些唯一字段检查,如果列表中不包含此项,请添加

if (DataReceivedCollectionTr.FirstOrDefault(i => i.SymbolNameTr == SymbolNameTra) == null)
{
    DataReceivedCollectionTr.Add(new DataTr() { SymbolNameTr = SymbolNameTra, TypeTr = TypeTra, AmountTr = AmountTra, ProfitLossTr = ProfitLossTra,PosID = PosID });
}