使用Windows手机进行地理定位,等待错误

时间:2013-04-15 01:08:53

标签: asynchronous geolocation windows-phone-8 async-await

我正在尝试获取用户的当前位置,但代码显示错误...

// try get user location
Geolocator geo = new Windows.Devices.Geolocation.Geolocator();
geo.DesiredAccuracyInMeters = 10;
try {
    Geoposition position = await geo.GetGeopositionAsync();
} catch (Exception e) { }

我的问题是编译器在await geo.GetGeopositionAsync();

显示错误
  

'await'运算符只能在其包含方法或lambda表达式使用'async'修饰符标记时使用。

但是从我所看到的,这是相同的代码as shown by Microsoft

我已经重新启动了VS12和电脑(这是一个虚拟机,但仍然),错误仍在这里...... 出了什么问题?

我也试过这个但仍然得到同样的错误:

 Geoposition position = await geo.GetGeopositionAsync().asTask<Geoposition>();

1 个答案:

答案 0 :(得分:3)

您需要使用async标记您的方法:

void **async** themethod()
    {
    // try get user location
    Geolocator geo = new Windows.Devices.Geolocation.Geolocator();
    geo.DesiredAccuracyInMeters = 10;
    try {
        Geoposition position = await geo.GetGeopositionAsync();
    } catch (Exception e) { }
}