同步调用异步方法时出错

托尼·维塔比(Tony Vitabile)

我在一个桌面应用程序中编写代码,该应用程序将在警车中使用,以使用新的Windows Geolocation API。我已经为该Geolocator.PositionChanged事件编写了一个事件监听器,但是没有被调用。在我阅读文档时,似乎Gelocator只有在位置发生变化时才会引发此事件。

我认为程序完成设置事件侦听器后,我应该做的第一件事是调用该Geolocator.GetPositionAsync方法以获取当前位置,然后让该位置在发生情况时得到更新。

我需要Geolocator.GetPositionAsync同步调用该方法。为此,我编写了以下方法:

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

但是,我在其中得到了以下编译器错误await

'await'要求类型'Windows.Foundation.IAsyncOperation'具有合适的GetAwaiter方法。您是否缺少针对“系统”的using指令?

这是文件顶部的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;

如何解决此编译器错误?

编辑

在添加System.Runtime.WindowsRuntime.dll对项目的引用后,错误消失了。但是,现在我遇到了一个新错误:

'System.Runtime.CompilerServices.TaskAwaiter`1 <Windows.Devices.Geolocation.Geoposition>'不包含'IsCompleted'的定义

我该如何解决这个问题?

编辑

这是调用该方法的代码:

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;
}
托尼·维塔比(Tony Vitabile)

谢谢你斯科特的帮助。我能够从您的建议中找出要添加引用的DLL,然后尝试尝试失败。

要解决此问题,我需要添加对以下内容的引用:

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

一旦这样做,错误就消失了。

我希望MS在其文档中添加有关使用VS2012中的Windows 8 API需要引用哪些DLL的信息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

同步功能内的异步调用

来自分类Dev

如何同步调用异步(等待)方法?

来自分类Dev

同步调用异步方法

来自分类Dev

调用异步方法时,StreamWriter使用同步调用

来自分类Dev

lua同步调用C异步函数

来自分类Dev

异步调用的同步循环

来自分类Dev

在内进行异步调用以同步

来自分类Dev

关于从异步方法同步调用受CPU约束的方法的困惑

来自分类Dev

Scala将异步调用与Future同步

来自分类Dev

异步调用同步方法

来自分类Dev

从异步调用void同步方法-无法等待void

来自分类Dev

如何从石英计划作业中同步调用异步方法

来自分类Dev

使科特林异步调用同步

来自分类Dev

还需要同步调用支持时的C#异步

来自分类Dev

同步调用异步函数时被调用者的线程会发生什么

来自分类Dev

如何同步调用异步(等待)方法?

来自分类Dev

异步调用方法

来自分类Dev

同步异步发出的异步调用

来自分类Dev

异步调用WCF方法

来自分类Dev

lua同步调用C异步函数

来自分类Dev

在内进行异步调用以同步

来自分类Dev

异步调用同步方法比自然异步方法更快地完成任务

来自分类Dev

同步CasperJS操作期间的异步调用

来自分类Dev

实体框架6-强制执行异步查询,编译时阻止同步调用

来自分类Dev

异步进程上的同步调用

来自分类Dev

异步调用WCF方法

来自分类Dev

使同步调用异步时如何返回TaskStatus?

来自分类Dev

如何从 C# 中的同步调用异步方法?

来自分类Dev

同步调用 webAPI 方法