Web服务中的异步调用

时间:2012-05-28 10:10:31

标签: c# web-services windows-phone-7 asynchronous

我正在为我的wp7应用程序使用Web服务,并且我很难找到对服务的异步调用是否返回值。我有一个验证取决于响应(结果)。以下是用于调用Web服务的代码片段以及应对UI进行的相应更改。

private void TLP_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    objevnt.indexChanged(1, lpcountry.SelectedIndex, "depart");
    if(objevnt.CName.Count>0)
    lpcity.ItemsSource = objevnt.GetCityDetails();
}
public void indexChanged(int Travel,int index,string journey)
{
    string slCountry = null;
    switch (Travel)
    {
        case 1:
            slCountry = lstCtryDetails.lstCtrylist[index].countryId.ToString();
            //= "depart";
            travelMode = journey;
            break;
        case 2:
                slCountry = lstCtryDetails.lstCtrylist[index].countryId.ToString();
                travelMode = journey;
                break;
        case 3:
                slCountry = lstCtryDetails.lstCtrylist[index].countryId.ToString();
                travelMode = journey;
                break;
    }

    GetCities = "http://Solutions/mobileservice/Citylist/countrycode/" + slCountry;
    WebClient myClientcity = new WebClient();
    myClientcity.DownloadStringAsync(new Uri(GetCities, UriKind.RelativeOrAbsolute));
    myClientcity.DownloadStringCompleted += new DownloadStringCompletedEventHandler(myClientcity_DownloadStringCompleted);
}

private void myClientcity_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    string _Countries = null;
    if (e.Error == null)
    {
        _Countries = e.Result;
        parseCtry(_Countries);
    }
}

private void parseCtry(string xml)
{
    XDocument myXdoc = XDocument.Load(new StringReader(xml));
    IEnumerable<XElement> lstElm = myXdoc.Descendants("GetCityList");
    lstCtryDetails.lstCitylist.Clear();
    foreach (XElement ele in lstElm.Elements())
    {
        if (ele.Name.ToString() != "Message")
        {
            // Fetch t Details
            if (!ele.IsEmpty && ele.Name.ToString() == "City")
            {
                lstCtryDetails.lstCitylist.Add(new cityList() { CityId = ele.Element("CityCode").Value, CityName = ele.Element("CityName").Value, CityLatitude = ele.Element("Latitude").Value, CityLongitude = ele.Element("Longitude").Value });
                CName.Add(ele.Element("CityName").Value);
                //Countchk = true;
            }
        }
    }

    lsCity = lstCtryDetails.lstCitylist;

    //chkloop = true;
}

public List<cityList> GetCityDetails()
{
    if (lsCity.Count > 0)
        return lsCity;
    return null;
}

现在我需要从getcitydetails()方法获取值列表。但由于异步调用,这会重新生成null。如何获取列表的计数,进行适当的验证。提前谢谢。

2 个答案:

答案 0 :(得分:0)

您是否尝试在执行webrequest之前添加回调?

myClientcity.DownloadStringCompleted += new DownloadStringCompletedEventHandler(myClientcity_DownloadStringCompleted);
myClientcity.DownloadStringAsync(new Uri(GetCities, UriKind.RelativeOrAbsolute));

===更新===

从你的评论我认为我理解这个问题。您想要的是data bindObservableCollection<T>到您的UI元素lpCity。例如,如果lpCity数据绑定到lsCity(实现ObservableCollection<T>),则通过异步Web请求更新lpCity后,UI元素将自动更新。

我会为你写出一个代码框架,我离开了我的Windows PC。

答案 1 :(得分:0)

您可能在ItemsSource返回结果之前设置了WebClient,也没有机会致电parseCtry

如果你搬家:

lpcity.ItemsSource = objevnt.GetCityDetails();

parseCtry的末尾(即注释//chkloop = true;所在的位置)然后它将评估为具有正确的计数 - 但是您可能遇到跨线程UI访问问题,但您应该是能够检查类似的东西(在DownloadStringCompleted开头):

if (Dispatcher.CheckAccess())
    Dispatcher.BeginInvoke(new DownloadStringCompletedEventHandler(myClientcity_DownloadStringCompleted), xml) // put us back on the UI thread if required

您最好将lsCity的类型更改为ObservableCollection<cityList>并绑定到它。