C#“等待”从桌面应用程序使用WinRT时出错 [英] C# "await" error when using WinRT from Desktop app
问题描述
我试图在C#中使用Windows运行时从桌面应用程序获取我的GPS位置。
我遵循此操作方法设置Visual Studio和此代码示例,以创建以下普通代码作为C#控制台应用程序: using System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading;
使用System.Threading.Tasks;
使用System.IO;
使用System.Runtime;
使用System.Windows;使用Windows的
;
使用Windows.Devices.Geolocation;
使用Windows.Foundation;
使用Windows.Foundation.Collections;
命名空间GeoLocCS
{
公共密封部分类程序
{
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());
}
}
}
在编译时,我得到错误:
错误1'await'需要类型'Windows.Foundation.IAsyncOperation< Windows.Devices.Geolocation。 Geoposition>有一个合适的GetAwaiter方法。你是否错过了'系统'的使用指令?
我尝试引用每个我可以的DLL,例如System.runtime,System.Runtime .WindowsRuntime,Windows.Foundation...
我缺少什么?
感谢答案,
Serge
我终于想通了我的问题:
- 我在8.1上,所以我在.csproj文件(targetingPlatform)中更改了这个选项,而不是以8.0为目标
- 在修改后,我可以引用Windows 我手动引用了以下两个DLL: $ b C:\程序文件(x86)\参考程序集\ Microsoft \ FrameFramework \\ Framework \ v4.5 \Facades\System.Runtime.dll
C:\ Windows \Microsoft®Microsoft®Windows®\\
I trying to get my GPS position from a Desktop app, using Windows Runtime, in C#.
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 :
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"...
What I am missing ?
Thanks for the answer,
Serge
I finally figured out my problem :
- I am on 8.1 so I changed this in the .csproj file (targetingPlatform) instead of targeting 8.0
- After that modification, I could reference "Windows"
I manually referenced the two following DLLs :
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.Runtime.dll
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Runtime.WindowsRuntime.dll
这篇关于C#“等待”从桌面应用程序使用WinRT时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!