WinRT GetGeopositionAsync在JavaScript中而不是在C ++中工作?

时间:2012-02-24 04:04:30

标签: windows-8 windows-runtime microsoft-metro

只是好奇这是一个错误还是我做错了。我正在尝试使用WinRT中的Geolocator。我在Javascript中有一个测试Metro应用程序,所有内容都适用于此代码:

var locator = Windows.Devices.Geolocation.Geolocator();
var promise = locator.getGeopositionAsync().then(
    function (pos) {
        Loc.innerText = "Lat: " +  pos.coordinate.latitude + ", Lng: " + pos.coordinate.longitude;
    });

我正在尝试使用此代码在C ++应用程序中执行相同的操作,但它不会输入我的lambda:

auto locator = ref new Geolocator();
auto operation = locator->GetGeopositionAsync();
operation->Completed =  ref new AsyncOperationCompletedHandler< Geoposition^ >(
    [=](IAsyncOperation<Geoposition^>^ operation)
    {
        auto result = operation->GetResults(); 
        std::wstringstream ss;
        ss << L"Lat: " << result->Coordinate->Latitude << L", Lng: " << result->Coordinate->Longitude;
        this->Loc->Text = ref new String( ss.str().c_str() );
    });

我已在两个应用中启用了“位置”功能。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用PPL。根据我的经验,使用C ++开发比使用这些处理程序要好得多。

#include <ppl>

Concurrency::task<Geoposition^> getPositionTask(locator->GetGeopositionAsync());
getPositionTask.then([=](IAsyncOperation<Geoposition^>^ operation) {
...

当然,如果你正在开发一个组件,你不能从你的方法返回Concurrency :: task对象,但只要你在你的代码中,它们可能是更好的。