【问题标题】:How can I wait for Async(geocodeService.GeocodeAsync) service/method to complete?如何等待 Async(geocodeService.GeocodeAsync) 服务/方法完成?
【发布时间】:2013-02-13 12:59:52
【问题描述】:

我正在使用 GeocodeService 获取两个地方的lattitudelongitude(从,到)

我得到了完美的纬度和经度,但我的问题是消息在 async 调用 (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,不是吗?
  • 我怎样才能做到这一点??为什么我的代码没有遵循方法调用的顺序(在 poini_click 事件中)?

标签: c# windows-phone-7 gps async-await


【解决方案1】:

创建另一个在异步完成加载时激活计时器的方法。

将 e.result 传递给它

让 timer.tick 检查 e.result 的长度是否 > 0

如果它在勾选显示窗口中

else 显示加载或其他内容

类似的东西

#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
}

【讨论】:

  • 感谢您的回答 :) 但是 geocodeService.GeocodeAsync 没有这个调用它不会调用 eocodeCompleted 方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-26
  • 2021-06-27
  • 2015-02-13
  • 2018-12-20
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多