c# - Windows Phone Web 访问 API 中的异步等待

标签 c# async-await windows-phone-8

WP8 是否支持异步/等待模式?

我需要从基于网络的 API 获取 XML,但看起来 WebClientWebRequest 不支持它。

在 WP8 BCL 中是否有支持 async/await 可用于 Web 访问的类?如果没有,是否有我可以使用的图书馆?

我知道创建包装器来支持它并不难,但这似乎应该包含在 SDK 中。

最佳答案

Are there classes that support async/await usable for web access in the WP8 BCL?

这是在 WP8 SDK 内测期间提出的一个问题,所以很遗憾,我可以回答说,没有。

但是正如您所提到的,制作您自己的包装器相当容易。

例如:

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

关于c# - Windows Phone Web 访问 API 中的异步等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13173614/

相关文章:

c# - C#属性类的继承

c# - c++ 和 c# sin 函数大值的不同结果

c# - System.Random 的多个实例是否仍然在 .Net Core 中使用相同的种子?

c# - 如何向编码的 UI 测试 (Windows Phone 8.1) 项目添加功能(在 C# 中)

c# - 为什么 WP8 应用程序中的 JsonConvert.DeserializeObject 可以工作,而 WP8 ScheduledTask 却不能

c# - 我该怎么做才能限制用户输入字母(a,b,c,d..)和其他字符(<,>,@<,!...)

c# - 入口点不能用 'async' 修饰符标记

swift - 如何在 Swift 5.5 中将 async/await 与 SwiftUI 一起使用?

multithreading - 为什么在这段代码中,await 不会阻塞 ui

visual-studio - 没有可启动的项目可用于分析