Web 服务中的异步调用
本文关键字:异步 调用 服务 Web | 更新日期: 2023-09-27 18:35:34
我正在为我的 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。 如何获取列表计数,以进行适当的验证。提前谢谢。
您可能在
WebClient
返回结果之前设置ItemsSource
,也没有机会调用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>
并绑定到它。
您是否尝试过在执行 Web 请求之前添加回调?
myClientcity.DownloadStringCompleted += new DownloadStringCompletedEventHandler(myClientcity_DownloadStringCompleted);
myClientcity.DownloadStringAsync(new Uri(GetCities, UriKind.RelativeOrAbsolute));
=== 更新 ===
从你的评论中,我想我理解这个问题。您想要的是将数据绑定ObservableCollection<T>
到您的 UI 元素lpCity
。例如,如果lpCity
数据绑定到lsCity
(实现ObservableCollection<T>
),一旦通过异步 Web 请求更新lpCity
,UI 元素将自动更新。
我会为你写出一个代码框架,我离开了我的Windows PC。