简体   繁体   English

异步等待Windows Phone Web访问API

[英]Async await in Windows Phone web access APIs

Is there support for the async/await pattern in WP8? 是否支持WP8中的async / await模式?

I need to get XML from a web-based API and it looks like that WebClient or WebRequest do not support it. 我需要从基于Web的API获取XML,看起来WebClientWebRequest不支持它。

Are there classes that support async/await usable for web access in the WP8 BCL? 是否有支持async / await的类可用于WP8 BCL中的Web访问? And if not, is there a library I can use? 如果没有,是否有可以使用的库?

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. 我知道创建包装器来支持它并不困难,但这似乎是一个包含在SDK中的东西。

Are there classes that support async/await usable for web access in the WP8 BCL? 是否有支持async / await的类可用于WP8 BCL中的Web访问?

This is a concern that has been raised during the closed beta of the WP8 SDK, so I can answer that unfortunately, no. 这是在WP8 SDK的封闭测试期间提出的一个问题,所以不幸的是,我可以回答这个问题。

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

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 . 作为本博客文章中描述的Microsoft.Bcl.Async NuGet包的一部分提供的Microsoft.Threading.Tasks.Extensions.Phone.dll对WP8有一些支持。

In particular, it includes WebClient.DownloadStringTaskAsync . 特别是,它包括WebClient.DownloadStringTaskAsync

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/ http://www.developer.nokia.com/Community/Wiki/Asynchronous_Programming_For_Windows_Phone_8我也发现此扩展程序有用http://nuget.org/packages/WP8AsyncWebClient/

Is there support for the async/await pattern in WP8? 是否支持WP8中的async / await模式?

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. 为了澄清,Windows Phone 8上有完整的C#5.0支持.WinRT API主要依赖于async / await,例如同步到PeopleHub或使用I / O API。

But the Silverlight and old .NET APIs have not been updated to use async/await for classes like the WebClient. 但Silverlight和旧的.NET API尚未更新为对WebClient等类使用async / await。

WP8 has native async/await support with some limitations, like missing HttpClient and possibly other classes. WP8具有本机异步/等待支持,但有一些限制,例如缺少HttpClient和可能的其他类。
WP7 had support for async/await in VS2010 using AsyncCTP, but in VS2012 it was re-added recently as Microsoft.Bcl.Async: WP7在VS2010中使用AsyncCTP支持async / await,但在VS2012中最近重新添加为Microsoft.Bcl.Async:
https://nuget.org/packages/Microsoft.Bcl.Async/1.0.12-beta https://nuget.org/packages/Microsoft.Bcl.Async/1.0.12-beta

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM