错误调用异步方法同步 [英] Error calling async method synchronously
问题描述
我在将警车被用来使用新的Windows地理位置API桌面应用程序中编写code。我写了 Geolocator.PositionChanged
事件的事件监听器,但它没有得到调用。在我的文档阅读,似乎 Gelocator
只引发此事件时位置的变化。
我的身影后,设置完事件侦听器我的程序应该做的第一件事就是打电话给 Geolocator.GetPositionAsync
方法来获得当前位置,然后让获取位置,因为它们发生更新。
我需要同步调用 Geolocator.GetPositionAsync
方法。为此,我写了下面的方法:
专用异步任务< Geoposition> GetCurrentPosition(){
等待Geolocator.GetGeopositionAsync();
}
不过,我得到就行了以下编译器错误与等待
在里面:
'等待'要求类型'Windows.Foundation.IAsyncOperation有一个合适的方法GetAwaiter。是否缺少using指令为系统?
块引用>下面是using语句在文件的顶部:
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Globalization;
使用System.Runtime.Serialization;
使用的System.Threading;
使用System.Threading.Tasks;
使用的System.Xml;
使用的System.Xml.Serialization;
使用Windows.Devices.Geolocation;如何解决此编译器错误?
修改
在我加入
System.Runtime.WindowsRuntime.dll
项目参考,错误就走开了。不过,现在我得到一个新的错误:
System.Runtime.CompilerServices.TaskAwaiter`1< Windows.Devices.Geolocation.Geoposition>'不包含IsCompleted
的定义
块引用>我如何解决这个问题呢?
修改
这里的code调用该方法:
保护覆盖无效初始化(){
//确保我们忘记任何previous Gelocator设备
如果(Geolocator!= NULL){
Geolocator.PositionChanged - = PositionChanged;
Geolocator.StatusChanged - = StatusChanged;
Geolocator = NULL;
} CurrentPosition =新GpsInformation(){时间戳= DateTime.Now};
Geolocator =新Geolocator();
Geolocator.DesiredAccuracy = PositionAccuracy.High; //发送位置更新精度最高可能。
Geolocator.MovementThreshold = 0; //发送位置更新不管车辆多远自上次更新以来感动。
Geolocator.ReportInterval = 0; //发送位置更新,因为他们成为availble的。
Geolocator.PositionChanged + = PositionChanged; //发送位置更新此方法。
Geolocator.StatusChanged + = StatusChanged; //发送状态改变的更新此方法。
开关(Geolocator.LocationStatus){
案例PositionStatus.Disabled:
案例PositionStatus.NotAvailable:
//该Geolocator设备被禁用。我们不会得到任何更新。抛出一个异常。
抛出新的异常(LPRCore不必使用Windows地理定位服务或地理定位服务不工作许可。);
} 任务< Geoposition> getPositionTask = GetCurrentPosition();
positionRecord = getPositionTask.Result;
}
解决方案感谢您斯科特的帮助。我能找出哪些DLL将引用添加到您的建议和放大器;然后打或错过尝试新事物。
要解决这个问题,我需要将引用添加到:
System.Threading.dll
和System.Threading.Tasks.dll
在我这样做,错误就走开了。
我希望微软会增加约哪些DLL需要引用使用从VS2012 Windows 8的API来他们的文档资料。
I'm writing code in a desktop application that will be used in police cars to use the new Windows Geolocation API. I've written an event listener for the
Geolocator.PositionChanged
event, but it's not getting called. In my reading of the documentation, it seems that theGelocator
only raises this event when the position changes.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.I need to call the
Geolocator.GetPositionAsync
method synchronously. 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' requires that the type 'Windows.Foundation.IAsyncOperation' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?
Here are the using statements at the top of the file:
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. However, now I'm getting a new error:'System.Runtime.CompilerServices.TaskAwaiter`1<Windows.Devices.Geolocation.Geoposition>' does not contain a definition for '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.
To fix the problem, I needed to add references to:
System.Threading.dll
andSystem.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.
这篇关于错误调用异步方法同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!