StorageFile NON Metro应用程序中的异步使用

我正在尝试在我的类库中创建一个StorageFile实例…

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

VS11没有建立说:’await’要求’Windows.Foundation.IAsyncOperation’类型具有合适的GetAwaiter方法。 你错过了’系统’的使用指令吗?

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

请告诉我你的想法…… Stelio

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

  8.0  

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

      

然后项目应该正常编译。

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

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

自从我添加引用后的post必须很长时间:
System.Runtime.WindowsRuntime.dll
System.Threading.Tasks.dll

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

   8.0  

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