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

Ste*_*llo 8 c# isolatedstorage async-await windows-8 windows-runtime

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

var localFolder = ApplicationData.Current.LocalFolder;
StorageFile destinationFile = await localFolder.CreateFileAsync(destination, CreationCollisionOption.GenerateUniqueName);
Run Code Online (Sandbox Code Playgroud)

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

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

请告诉我你的想法...... Stelio

Pan*_*fas 10

对我有用的是"手动"添加TargetPlatformVersion

<PropertyGroup>
  <TargetPlatformVersion>8.0</TargetPlatformVersion>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

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

<ItemGroup>
  <Reference Include="System.Runtime.WindowsRuntime" />
  <Reference Include="System.Runtime" />
  <Reference Include="Windows" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

那么项目应该正常编译.


Ans*_*s21 8

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


Ada*_*rth 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

其次,我创建了一个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
Run Code Online (Sandbox Code Playgroud)

然后,在命令提示符下,我只是运行EnumDevices.exe来查看输出.