刷新列表框

时间:2014-03-03 15:14:26

标签: windows-phone-7 windows-phone

我已经发布了我的问题,我希望这次我会得到一个答案,我正在尝试通过在列表框中使用变量foreach elment刷新我的列表框但是我只获得了新元素而其他项目删除了她的我的代码:

private void refresh()
{
    RestClient client = new RestClient();
    client.BaseUrl = "http";

    RestRequest request = new RestRequest();

    request.Method = Method.GET;
    request.AddParameter("action", "atms");

    request.AddParameter("lang", "ar");
    request.AddParameter("mode", "xml");
    request.AddParameter("appli", "WP");
    request.AddParameter("city_id", selectedState._id);
    client.ExecuteAsync(request, ParseFeedCallBack_ListAgence);
}

public void ParseFeedCallBack_ListAgence(IRestResponse response)
{
    if (response.StatusCode == HttpStatusCode.OK)
    {
        ParseXMLFeedAgences(response.Content);
    }
}

private void ParseXMLFeedAgences(string feed)
{
    if (feed == null)
        return;

    try
    {
        XElement xmlItems = XElement.Parse(feed);
        XDocument xmlItems2 = XDocument.Parse(feed);
        ResultSearch_List.ItemsSource =
            from response in xmlItems.Descendants("result")
            let add = response.Element("Address")
            let idagence = response.Element("id")
            let latt = response.Element("Latitude")
            let longt = response.Element("Longitude")

            select new State
            {
                Address = add == null ? null : add.Value,
                _idagence = idagence == null ? null : idagence.Value,
                Latitude = latt == null ? null : latt.Value,
                Longitude = longt == null ? null : longt.Value,
            };

        foreach (var resp in xmlItems2.Descendants("result"))
        {
            RestClient client = new RestClient();
            client.BaseUrl = "http:";

            RestRequest request = new RestRequest();

            request.Method = Method.GET;
            request.AddParameter("action", "atms_distances");
            request.AddParameter("lang", "ar");
            request.AddParameter("mobile_latitude", "35.843283");
            request.AddParameter("mobile_longitude", "10.61617");
            request.AddParameter("atm_latitude", "35.2579090");
            request.AddParameter("atm_longitude", "10.7992144");

            client.ExecuteAsync(request, response =>
                {
                    ParseXMLFeedDistance(response.Content);
                });
        }
    }
    catch (WebException)
    {
    }
}

private void ParseXMLFeedDistance(string feed)
{
    if (feed == null)
        return;

    try
    {
        XDocument xmlItems2 = XDocument.Parse(feed);

        var list = new List<State>();

        foreach (XElement val in xmlItems2.Descendants("result").Last().Elements())
        {
            list.Add(new State
                {
                    TotalDistance = val.Value,
                });
        }

        ResultSearch_List.ItemsSource = list;
    }
    catch
    {
    }
}

她是我的代码,请告诉我应该改变什么:

foreach (XElement val in xmlItems2.Descendants("result"))
            {


                states.Add(new State
                {
                    Address = val.Element("Address").Value,
                    _idagence = val.Element("CityId").Value,
                    Latitude = val.Element("Latitude").Value,
                    Longitude = val.Element("Longitude").Value,

                });
            }

            foreach (var resp in xmlItems2.Descendants("result"))
            {
                RestClient client = new RestClient();
                client.BaseUrl = "http://";

                RestRequest request = new RestRequest();

                request.Method = Method.GET;
                request.AddParameter("action", "atms_distances");

                request.AddParameter("mode", "xml");
                request.AddParameter("appli", "WP");
                request.AddParameter("mobile_latitude", "35.843283");
                request.AddParameter("mobile_longitude", "10.61617");
                request.AddParameter("atm_latitude", "35.2579090");
                request.AddParameter("atm_longitude", "10.7992144");

                client.ExecuteAsync(request, response =>
                {

                    XDocument xmlItems22 = XDocument.Parse(response.Content);


                    foreach (XElement val in xmlItems22.Descendants("result").Last().Elements())
                    {

                        //  distance = val.Value;
                        states.Add(new State
                        {
                            TotalDistance = val.Value,
                        });
                    }
                    ResultSearch_List.ItemsSource = states;
                });
            }

1 个答案:

答案 0 :(得分:0)

请勿使用List作为数据源,而是使用ObservableCollection

// keep this in your class
ObservableCollection<State> States { get; set; }

// add to constructor
States = new ObservableCollection<State>();

现在将最后一个函数(ParseXMLFeedDistance)更改为:

private void ParseXMLFeedDistance(string feed)
{
    if (feed == null)
        return;

    try
    {
        XDocument xmlItems2 = XDocument.Parse(feed);

        foreach (XElement val in xmlItems2.Descendants("result").Last().Elements())
        {
            // distance = val.Value;
            list.Add(new State
            {
                TotalDistance = val.Value,
            });
        }
    }
    catch {}
}

不要重置列表,你总是在摧毁它并从头开始创建。