便携式类库不能使用的await为Win 8和Win 8手机 [英] Cannot use await in Portable Class Library for Win 8 and Win Phone 8

查看:197
本文介绍了便携式类库不能使用的await为Win 8和Win 8手机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Visual Studio 2012创建一个可移植类库用于在Windows 8商店应用和一款Windows Phone 8应用。

I'm attempting to create a Portable Class Library in Visual Studio 2012 to be used for a Windows 8 Store app and a Windows Phone 8 app.

我收到以下错误:

等待要求的类型
  Windows.Foundation.IAsyncOperation
  具有合适GetAwaiter方法​​。是否缺少using指令
  为系统?

'await' requires that the type 'Windows.Foundation.IAsyncOperation' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?

在这行code的:

StorageFolder guidesInstallFolder = await Package.Current.InstalledLocation.GetFolderAsync(guidesFolder);

我的便携式类库是针对.NET框架4.5的Windows Phone 8和.NET的Windows Store应用程序。

My Portable Class Library is targeted at .NET Framework 4.5, Windows Phone 8 and .NET for Windows Store apps.

我没有得到这个错误在纯Windows Phone 8的项目这一行code,我不明白这一点在Windows商店应用要么,所以我不明白为什么它不会在我的PCL工作。

I don't get this error for this line of code in a pure Windows Phone 8 project, and I don't get it in a Windows Store app either so I don't understand why it won't work in my PCL.

该GetAwaiter是班上WindowsRuntimeSystemExtensions这是System.Runtime.WindowsRuntime.dll扩展方法。使用对象浏览器,我可以看到这个DLL可以在 .NET的Windows Store应用程序的组件设置和在的Windows Phone 8 的组件设置,但不能在 .NET可移植的子集的。我只是不明白为什么它会不会在便携式子集,如果它在我的两个目标平台提供。

The GetAwaiter is an extension method in the class WindowsRuntimeSystemExtensions which is in System.Runtime.WindowsRuntime.dll. Using the Object Browser I can see this dll is available in the .NET for Windows Store apps component set and in the Windows Phone 8 component set but not in the .NET Portable Subset. I just don't understand why it wouldn't be in the Portable Subset if it's available in both my targeted platforms.

推荐答案

您需要异步目标定位包上的NuGet这里异步/等待为目标的联合工作。

You need the Async targetting pack on NuGet here for async/await to work for that combination of targets.

更新:

试试这个(废话)code段如果使用异步检查/等待正确。

Try this (nonsense) code snippet to check if it is using async/await correctly.

public async void MyMethodAsync()
{
    var req = WebRequest.Create("");
    await req.GetRequestStreamAsync();
}

不过,即使你过去异步/的await 不被用,套餐 API,你的第一个问题呼吁是不是在PCL可用。

However even if you get past the first problem of async/await not being available, the Package API you are calling is not available in the PCL.

这篇关于便携式类库不能使用的await为Win 8和Win 8手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆