1

我正在为我的 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 。如何获取列表计数,进行适当的验证。提前致谢。

4

2 回答 2

0

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

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

=== 更新 ===

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

我会为你写一个代码框架,我不在我的 Windows PC 上。

于 2012-05-28T11:35:44.560 回答
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为 anObservableCollection<cityList>并绑定到它。

于 2012-05-28T15:27:04.713 回答