8

我正在尝试在我的类库中创建 StorageFile 的实例...

var localFolder = ApplicationData.Current.LocalFolder;
StorageFile destinationFile = await localFolder.CreateFileAsync(destination, CreationCollisionOption.GenerateUniqueName);

VS11 is not building say: 'await' 要求类型 'Windows.Foundation.IAsyncOperation' 有一个合适的 GetAwaiter 方法。您是否缺少“系统”的使用指令?

显然我使用 .net 4.5 作为目标并且我正在引用 Windows 程序集......不知道为什么这段代码在 MetroStyle 中工作但不在类库中构建......如何在类库中创建 Storagefile 的实例? ?? 在这个阶段,是否以异步方式创建文件并不重要......

请让我知道你的想法...... Stelio

4

4 回答 4

10

对我有用的是“手动”添加 TargetPlatformVersion

<PropertyGroup>
  <TargetPlatformVersion>8.0</TargetPlatformVersion>
</PropertyGroup>

并在项目组中添加以下内容

<ItemGroup>
  <Reference Include="System.Runtime.WindowsRuntime" />
  <Reference Include="System.Runtime" />
  <Reference Include="Windows" />
</ItemGroup>

然后项目应该正常编译。

于 2013-11-01T17:17:25.683 回答
8

我有同样的问题。问题是命名空间标头中缺少系统命名空间。我只是在命名空间中包含了系统并且它工作。希望能帮助到你。

于 2014-04-25T10:35:09.953 回答
5

看起来您正在尝试使用 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 即可查看输出。

于 2012-05-03T09:30:42.817 回答
1

应该是很久没有发帖的时候我添加引用后:
System.Runtime.WindowsRuntime.dll
System.Threading.Tasks.dll

并在项目文件中定位 Windows 8:

  <PropertyGroup>
    <TargetPlatformVersion>8.0</TargetPlatformVersion>
  </PropertyGroup>

上面提到的例子可以在VS中编译。

于 2013-08-08T14:14:57.147 回答