2012-05-03 15 views
8

我想在我的类库创建StorageFile的实例...StorageFile异步使用在非地铁应用

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

VS11不建说:“等待”要求的类型“窗口。 Foundation.IAsyncOperation'有一个合适的GetAwaiter方法。你是否错过了'系统'的使用指令?

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

请让我知道你的想法... 斯泰利奥

+0

在Metro风格中,代码文件顶部是否还有额外的使用语句? –

+0

有相同的使用... –

回答

5

看起来你正试图从WinRT的使用类型因为StorageFileclass documentation指出它仅适用于Metro,它在Windows.Storage中找到。

This blog post经历了如何构建它,但它似乎是一个手动过程。它还详细介绍了错误的原因:

Using the await keyword causes the compiler to look for a GetAwaiter method on this interface. Since IAsyncOperation does not define a GetAwaiter method, the compiler wants to look for an extension method.

基本上,它看起来像你需要添加一个引用: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来查看输出。

+0

我认为应该指出什么Jeffrey Richter在他的帖子中提到:缺少的程序集是System.Runtime.WindowsRuntime.dll。 – svick

+0

伙计们,感谢您的及时响应...我试图使用Windows 8上的WPF应用程序的背景下载器(在NON-Metro上下文中),但我想这不是一个好主意,最好是使用BITS ....但我认为与windowsRuntime使用interop不是一个好主意... –

+0

尝试从博客运行示例时出现以下错误:'numDevices.cs(14,35):error CS4028 :'await'要求类型 'Windows.Foundation.IAsyncOperation '有一个合适的GetAwaiter方法。您是否缺少 'System'的使用指令? – Hjulle

1

它必须是因为后很长一段时间,当我 添加引用后:
System.Runtime.WindowsRuntime.dll
System.Threading.Tasks.dll

,并针对Windows 8项目文件:

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

上面提到的例子可以编译成VS.

+0

如果你想问一些问题,请发布一个单独的问题。 – thumbmunkeys

10

什么工作对我来说是“手工”添加TargetPlatformVersion

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

,并在项目组中添加以下

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

那么该项目应normaly编译。

+1

谢谢soooo多!!!这应该被标记为答案 –

8

我有同样的问题。问题是名称空间头文件中缺少系统名称空间。我只是将系统包含在名称空间中并且它工作正常。希望能帮助到你。

+1

这实际上为我工作。 –