无法在 Win 8 和 Win Phone 8 的便携式类库中使用 await [英] Cannot use await in Portable Class Library for Win 8 and Win Phone 8

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

问题描述

我正在尝试在 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.

我收到以下错误:

'await' 要求类型'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'?

在这行代码:

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

我的可移植类库针对 .NET Framework 4.5、Windows Phone 8 和 .NET for 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 项目中没有收到这行代码的错误,也没有在 Windows Store 应用程序中收到它,所以我不明白为什么它在我的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 for 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.

更新:

试试这个(废话)代码片段来检查它是否正确使用了 async/await.

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();
}

但是,即使您解决了 async/await 不可用的第一个问题,您正在调用的 Package 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.

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

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