看起来您正在尝试使用 WinRT 库中的类型,因为StorageFile
类文档声明它仅适用于 Metro 并且它位于Windows.Storage
.
这篇博客文章介绍了如何构建它,但它似乎是一个手动过程。它还详细说明了错误的原因:
使用 await 关键字会导致编译器在此接口上查找 GetAwaiter 方法。由于 IAsyncOperation 没有定义 GetAwaiter 方法,编译器想要寻找扩展方法。
基本上,看起来您需要添加对以下内容的引用:System.Runtime.WindowsRuntime.dll
请花时间阅读他的博客文章,但为了清楚起见,我将把重要的部分放在这里。
以下博客内容被毫不客气地抄袭
首先,在记事本中,我在 EnumDevices.cs 中创建了以下 C# 源代码:
using System;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Foundation;
class App {
static void Main() {
EnumDevices().Wait();
}
private static async Task EnumDevices() {
// To call DeviceInformation.FindAllAsync:
// Reference Windows.Devices.Enumeration.winmd when building
// Add the "using Windows.Devices.Enumeration;" directive (as shown above)
foreach (DeviceInformation di in await DeviceInformation.FindAllAsync()) {
Console.WriteLine(di.Name);
}
}
}
其次,我创建了一个 Build.bat 文件,我从开发人员命令提示符运行该文件以构建此代码(这应该是 1 行,但我将其包装在这里以供阅读):
csc EnumDevices.cs
/r:c:\Windows\System32\WinMetadata\Windows.Devices.Enumeration.winmd
/r:c:\Windows\System32\WinMetadata\Windows.Foundation.winmd
/r:System.Runtime.WindowsRuntime.dll
/r:System.Threading.Tasks.dll
然后,在命令提示符下,我只需运行 EnumDevices.exe 即可查看输出。