Async await in Windows Phone web access APIs

前端 未结 5 1041
闹比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:01

    I had the same issue and I found this and helped me

    private async Task<T> ExecuteAsync<T>(RestRequest request)
        {
            var tcs = new TaskCompletionSource<T>();
            _client.ExecuteAsync(request, resp =>
            {
                var value = JsonConvert.DeserializeObject<T>(resp.Content);
                if (value.ErrorCode > 0)
                {
                    var ex = new ToodledoException(value.ErrorCode, value.ErrorDesc);
                    tcs.SetException(ex);
                }
                else
                    tcs.SetResult(value);
            });
            return await tcs.Task;
        }
    

    http://www.developer.nokia.com/Community/Wiki/Asynchronous_Programming_For_Windows_Phone_8 also I found this extension helpful http://nuget.org/packages/WP8AsyncWebClient/

    0 讨论(0)
  • 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<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;
        }
    }
    
    0 讨论(0)
  • 2020-12-29 18:07

    There is some support for WP8 in the Microsoft.Threading.Tasks.Extensions.Phone.dll provided as part of the Microsoft.Bcl.Async NuGet package described in this blog post.

    In particular, it includes WebClient.DownloadStringTaskAsync.

    0 讨论(0)
  • 2020-12-29 18:08

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

    Just to clarify, there is full C# 5.0 support on Windows Phone 8. The WinRT APIs largely depend on async/await, such as syncing to PeopleHub, or using the I/O APIs.

    But the Silverlight and old .NET APIs have not been updated to use async/await for classes like the WebClient.

    0 讨论(0)
  • 2020-12-29 18:11

    WP8 has native async/await support with some limitations, like missing HttpClient and possibly other classes.
    WP7 had support for async/await in VS2010 using AsyncCTP, but in VS2012 it was re-added recently as Microsoft.Bcl.Async:
    https://nuget.org/packages/Microsoft.Bcl.Async/1.0.12-beta

    0 讨论(0)
提交回复
热议问题