2013-02-13 23 views
0

現在用的是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 
    } 
+0

你實際上不能在WP7的C#5.0中使用'async'''await',不是嗎? – svick 2013-02-13 13:50:33

+0

我怎麼能做到這一點?爲什麼我的代碼沒有按照方法調用的順序(在poini_click事件中)? – sunny 2013-02-13 14:02:05

回答

1

創建完成異步被加載時激活一個計時器的另一方法。

傳的e.result

有timer.tick檢查,看看是否e.result是長度> 0

如果是靠賒賬櫥窗

其他節目加載或什麼曾經

像這樣的事情

#pretend code# 
Global Variable -> string location; 

getGeoLoc += new geoLocCompleted; 

void geoLocCompleted(sender, e){ 
    location = e.result 
    Timer time = new Timer(): 
    time.tick += OnTick; 

}

void onTick(send, e){ 
    if(e.result.length > 0) 
     Show your results 
    else 
     Show loading dialog 
} 
+0

感謝您的回答:)但是'geocodeService.GeocodeAsync'沒有這個調用它沒有調用'eocodeCompleted'方法 – sunny 2013-02-14 08:02:29