[英]C# “await” error when using WinRT from Desktop app
I trying to get my GPS position from a Desktop app, using Windows Runtime, in C#. 我试图在Windows#中使用C#从桌面应用程序获取GPS位置。
I followed this how-to to set up Visual Studio and this code sample to create the following trivial code as a C# console app : 我遵循此操作方法来设置Visual Studio和此代码示例,以将以下简单代码创建为C#控制台应用程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime;
using System.Windows;
using Windows;
using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.Foundation.Collections;
namespace GeoLocCS
{
public sealed partial class Program
{
static void Main(string[] args)
{
Test test = new Test();
Console.Read();
}
}
public class Test
{
private Geolocator geolocator;
public Test()
{
Console.WriteLine("test");
geolocator = new Geolocator();
this.getPos();
}
async void getPos()
{
Geoposition pos = await geolocator.GetGeopositionAsync();
Console.WriteLine(pos.Coordinate.Latitude.ToString());
}
}
}
When trying to compile, I get the error : 尝试编译时,出现错误:
Error 1 'await' requires that the type 'Windows.Foundation.IAsyncOperation<Windows.Devices.Geolocation.Geoposition>' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?
I tried to reference every DLL that I could, like "System.runtime", "System.Runtime.WindowsRuntime", "Windows.Foundation"... 我试图引用每个可能的DLL,例如“ System.runtime”,“ System.Runtime.WindowsRuntime”,“ Windows.Foundation” ...
What I am missing ? 我想念什么?
Thanks for the answer, 感谢您的回答,
Serge 哔叽
I finally figured out my problem : 我终于找到了我的问题:
I manually referenced the two following DLLs : 我手动引用了以下两个DLL:
C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework.NETFramework\\v4.5\\Facades\\System.Runtime.dll C:\\ Program Files(x86)\\参考程序集\\ Microsoft \\ Framework.NETFramework \\ v4.5 \\ Facades \\ System.Runtime.dll
C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\System.Runtime.WindowsRuntime.dll C:\\ WINDOWS \\ Microsoft.NET \\ Framework64 \\ v4.0.30319 \\ System.Runtime.WindowsRuntime.dll
If you are On Win8.1 Please Use: 如果您使用的是Win8.1,请使用:
<TargetPlatformVersion>8.1</TargetPlatformVersion>
and not 8.0. 而不是8.0。
Adding to the existing answers: 添加到现有答案中:
I suddenly got this problem with a multiproject solution in WPF (still don't know why). 我突然在WPF中使用多项目解决方案遇到了这个问题(仍然不知道为什么)。 I tried switching between TargetPlatformVersions 8.1 and 10.0, tried using the v4.5- and v4.5.1-System.Runtime.WindowsRuntime.dll, always alternating between BadImageFormatException and FileNotFoundException.
我尝试在TargetPlatformVersions 8.1和10.0之间切换,尝试使用v4.5-和v4.5.1-System.Runtime.WindowsRuntime.dll,始终在BadImageFormatException和FileNotFoundException之间交替。
Turned out to be references in some of the app.config-files (not the .csproj-files!). 原来是某些app.config文件中的引用(不是.csproj文件!)。 Visual Studio 2017 must have added them at some point and as soon as I removed these entries, the above solutions finally worked.
Visual Studio 2017必须在某个时候添加了它们,并且一旦我删除了这些条目,上述解决方案便开始起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.