'Windows.Storage.StorageFile File = await FilePicker.PickSingleFileAsync()' 不工作

'Windows.Storage.StorageFile File = await FilePicker.PickSingleFileAsync()' not working

我有以下代码在应用程序中显示文件选取器:

var FilePicker = new Windows.Storage.Pickers.FileOpenPicker();
FilePicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
FilePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.ComputerFolder;
FilePicker.FileTypeFilter.Add(".pcs");
FilePicker.FileTypeFilter.Add(".pcp");

Windows.Storage.StorageFile File = await FilePicker.PickSingleFileAsync();

但是,Windows.Storage.StorageFile File = await FilePicker.PickSingleFileAsync()在编译过程中导致了这个错误:

Error CS4036 'IAsyncOperation<StorageFile>' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'IAsyncOperation<StorageFile>' could be found (are you missing a using directive for 'System'?)

为什么会这样?我从 MSDN 获得了代码。有人可以帮我吗?

注意:我正在为 Universal Windows 编程。

您在使用中遗漏了对 System 的明显引用。

using System;

你为什么需要这个参考,为什么它抱怨它缺少一个看似未使用的方法?

使用 await,它实际上调用了 WindowsRuntimeSystemExtensions.GetAwaiter,这是 IAsyncOperation 的扩展方法(让 TaskAwaiter 等待)。由于 WindowsRuntimeSystemExtensions 驻留在 System 命名空间中,因此您需要 using 来获取扩展方法。