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