Async await in Windows Phone web access APIs

前端 未结 5 1058
闹比i
闹比i 2020-12-29 17:57

Is there support for the async/await pattern in WP8?

I need to get XML from a web-based API and it looks like that WebClient or WebRequest

5条回答
  •  执念已碎
    2020-12-29 18:02

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

    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.

    For instance:

    public static class Extensions
    {
        public static Task DownloadStringTask(this WebClient webClient, Uri uri)
        {
            var tcs = new TaskCompletionSource();
    
            webClient.DownloadStringCompleted += (s, e) =>
            {
                if (e.Error != null)
                {
                    tcs.SetException(e.Error);
                }
                else
                {
                    tcs.SetResult(e.Result);
                }
            };
    
            webClient.DownloadStringAsync(uri);
    
            return tcs.Task;
        }
    }
    

提交回复
热议问题