简体   繁体   English

同步调用异步方法时出错

[英]Error calling async method synchronously

I'm writing code in a desktop application that will be used in police cars to use the new Windows Geolocation API. 我在一个桌面应用程序中编写代码,该应用程序将在警车中使用,以使用新的Windows Geolocation API。 I've written an event listener for the Geolocator.PositionChanged event, but it's not getting called. 我已经为Geolocator.PositionChanged事件编写了一个事件监听器,但是没有被调用。 In my reading of the documentation, it seems that the Gelocator only raises this event when the position changes. 在我阅读文档时,似乎Gelocator仅在位置更改时引发此事件。

I figure the first thing my program should do after it finished setting up the event listener is to call the Geolocator.GetPositionAsync method to get the current position and then let the position get updated as they happen. 我认为程序完成设置事件侦听器后,应该做的第一件事是调用Geolocator.GetPositionAsync方法来获取当前位置,然后让该位置在发生时更新。

I need to call the Geolocator.GetPositionAsync method synchronously. 我需要同步调用Geolocator.GetPositionAsync方法。 To that end, I wrote the following method: 为此,我编写了以下方法:

private async Task<Geoposition> GetCurrentPosition() {
    await Geolocator.GetGeopositionAsync();
}

However, I get the following compiler error on the line with await in it: 但是,我在其中await的行中收到以下编译器错误:

'await' requires that the type 'Windows.Foundation.IAsyncOperation' have a suitable GetAwaiter method. “ await”要求类型“ Windows.Foundation.IAsyncOperation”具有合适的GetAwaiter方法。 Are you missing a using directive for 'System'? 您是否缺少针对“系统”的using指令?

Here are the using statements at the top of the file: 这是文件顶部的using语句:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using Windows.Devices.Geolocation;

How do I fix this compiler error? 如何解决此编译器错误?

Edit 编辑

After I added a reference to System.Runtime.WindowsRuntime.dll the project, the error went away. 在添加对System.Runtime.WindowsRuntime.dll项目的引用后,错误消失了。 However, now I'm getting a new error: 但是,现在我遇到了一个新错误:

'System.Runtime.CompilerServices.TaskAwaiter`1<Windows.Devices.Geolocation.Geoposition>' does not contain a definition for 'IsCompleted' 'System.Runtime.CompilerServices.TaskAwaiter`1 <Windows.Devices.Geolocation.Geoposition>'不包含'IsCompleted'的定义

How do I fix this one? 我该如何解决这个问题?

Edit 编辑

Here's the code that calls the method: 这是调用该方法的代码:

protected override void Initialize() {
    // Make sure we "forget" any previous Gelocator device
    if ( Geolocator != null ) {
        Geolocator.PositionChanged -= PositionChanged;
        Geolocator.StatusChanged   -= StatusChanged;
        Geolocator                  = null;
    }

    CurrentPosition = new GpsInformation() { TimeStamp = DateTime.Now };
    Geolocator = new Geolocator();
    Geolocator.DesiredAccuracy   = PositionAccuracy.High;    // Sends position updates with the highest accuracy possible.
    Geolocator.MovementThreshold = 0;                        // Sends position updates no matter how far the vehicle has moved since the last update.
    Geolocator.ReportInterval    = 0;                        // Sends position updates as they become availble.
    Geolocator.PositionChanged  += PositionChanged;          // Sends position updates to this method.
    Geolocator.StatusChanged    += StatusChanged;            // Sends status changed updates to this method.
    switch ( Geolocator.LocationStatus ) {
        case PositionStatus.Disabled:
        case PositionStatus.NotAvailable: 
            // The Geolocator device is disabled.  We won't get any updates.  Throw an exception.
            throw new Exception( "LPRCore does not have permission to use Windows Geolocation services or Geolocation services are not working." );
    }

    Task<Geoposition> getPositionTask = GetCurrentPosition();
    positionRecord = getPositionTask.Result;
}

Thank you Scott for your help. 谢谢你斯科特的帮助。 I was able to figure out which DLLs to add references to from your suggestions & then hit-or-miss trying things. 我能够从您的建议中找出要添加引用的DLL,然后尝试尝试失败。

To fix the problem, I needed to add references to: 要解决此问题,我需要添加对以下内容的引用:

  • System.Threading.dll and System.Threading.dll
  • System.Threading.Tasks.dll

Once I did this, the error went away. 一旦这样做,错误就会消失。

I wish MS would add information about which DLLs need to be referenced to use the Windows 8 APIs from VS2012 to their documentation. 我希望MS在其文档中添加有关使用VS2012中的Windows 8 API需要引用哪些DLL的信息。

可能存在重复问题,但如果尚未添加,则还应添加对System.Runtime.WindowsRuntime.dll的引用。

I think you ran into this problem while trying to resolve the wrong issue. 我认为您在尝试解决错误问题时遇到了这个问题。

While investigation to write my Exposing the Geolocator as a Reactive Service on the Nokia Developer Wiki I came across the same problem (no change events) and tried the same solution (calling Geolocator.GetGeopositionAsync ). 在进行调查以在Nokia Developer Wiki上编写“ 将Geolocator作为响应式服务公开”时,我遇到了相同的问题(无更改事件),并尝试了相同的解决方案(称为Geolocator.GetGeopositionAsync )。

It turns out that you need to set some properties ( Geolocator.MovementThreshold , Geolocator.DesiredAccuracy , Geolocator.ReportInterval ) with some values. 事实证明,您需要使用一些值来设置一些属性( Geolocator.MovementThresholdGeolocator.DesiredAccuracyGeolocator.ReportInterval )。

If you are not familiar with the Reactive Extensions (Rx) , you should try it. 如果您不熟悉Reactive Extensions(Rx) ,则应该尝试一下。 You can get my implementation from my CodeProject Workspace . 您可以从CodeProject工作区获取我的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM