現在用的是GeocodeService得到lattitude
和兩個地longitude
(從,到)如何等待Async(geocodeService.GeocodeAsync)服務/方法完成?
我得到的經度和緯度完美,但我的問題是該消息之前異步調用(geocodeService.GeocodeAsync
)如何顯示我等待完成異步調用(geocodeService_GeocodeCompleted
)??
我的代碼是
private void point_Click(object sender, RoutedEventArgs e)
{
getCoordinates(fromText.Text, 1);// this is calling after bellow message box :(
getCoordinates(toText.Text, 2);
MessageBox.Show(" completed "); // 1 why this message is displaying first before above calls
}
private void getCoordinates(string address, int index)
{
GeocodeRequest geocodeRequest = new GeocodeRequest();
// Set the credentials using a valid Bing Maps key
geocodeRequest.Credentials = new Credentials();
geocodeRequest.Credentials.ApplicationId = "ApgLkoHIG4rNShRJAxMMNettsv6SWs3eP8OchozFS89Vex7BRHsSbCr31HkvYK-d";
// Set the full address query
geocodeRequest.Query = address;
// Set the options to only return high confidence results
FilterBase[] filters = new FilterBase[1];
filters[0] = new ConfidenceFilter() { MinimumConfidence = Confidence.High };
GeocodeOptions geocodeOptions = new GeocodeOptions();
geocodeOptions.Filters = new ObservableCollection<FilterBase>(filters);
geocodeRequest.Options = geocodeOptions;
// Make the geocode request
GeocodeServiceClient geocodeService = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
geocodeService.GeocodeCompleted += new EventHandler<GeocodeCompletedEventArgs>(geocodeService_GeocodeCompleted);
geocodeService.GeocodeAsync(geocodeRequest, index);
}
void geocodeService_GeocodeCompleted(object sender, GeocodeCompletedEventArgs e)
{
GeocodeResponse geocodeResponse = e.Result;
fromLat = geocodeResponse.Results[0].Locations[0].Latitude;
fromLon = geocodeResponse.Results[0].Locations[0].Longitude;
Messagebox.Show("latitude : "+fromLat); // this is displaying randomly
}
你實際上不能在WP7的C#5.0中使用'async'''await',不是嗎? – svick 2013-02-13 13:50:33
我怎麼能做到這一點?爲什麼我的代碼沒有按照方法調用的順序(在poini_click事件中)? – sunny 2013-02-13 14:02:05