Windows Phone Web 访问 API 中的异步等待 [英] Async await in Windows Phone web access APIs
问题描述
WP8 是否支持 async/await 模式?
Is there support for the async/await pattern in WP8?
我需要从基于 Web 的 API 获取 XML,但看起来 WebClient
或 WebRequest
不支持它.
I need to get XML from a web-based API and it looks like that WebClient
or WebRequest
do not support it.
WP8 BCL 中是否有支持异步/等待的类可用于 Web 访问?如果没有,是否有我可以使用的库?
Are there classes that support async/await usable for web access in the WP8 BCL? And if not, is there a library I can use?
我知道创建包装器来支持它并不难,但这似乎是包含在 SDK 中的东西.
I know it is not that hard to create wrappers to support it, but this seems like a thing that would be included in the SDK.
推荐答案
WP8 BCL 中是否有支持 async/await 的类可用于 Web 访问?
Are there classes that support async/await usable for web access in the WP8 BCL?
这是在 WP8 SDK 内测期间提出的一个问题,所以我可以很遗憾地回答,不是.
This is a concern that has been raised during the closed beta of the WP8 SDK, so I can answer that unfortunately, no.
但正如您所提到的,制作自己的包装器相当容易.
But as you mentioned, it is reasonably easy to make your own wrappers.
例如:
public static class Extensions
{
public static Task<string> DownloadStringTask(this WebClient webClient, Uri uri)
{
var tcs = new TaskCompletionSource<string>();
webClient.DownloadStringCompleted += (s, e) =>
{
if (e.Error != null)
{
tcs.SetException(e.Error);
}
else
{
tcs.SetResult(e.Result);
}
};
webClient.DownloadStringAsync(uri);
return tcs.Task;
}
}
这篇关于Windows Phone Web 访问 API 中的异步等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!