WP8 SDK服务进口与参考不能基于任务的操作 [英] WP8 SDK import Service Reference with task-based operations not possible

查看:116
本文介绍了WP8 SDK服务进口与参考不能基于任务的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,这似乎与进口生成基于任务的操作,在VS2012服务引用不工作。它的操作系统变灰。

与WPF一个新的项目工作正常的测试 - 我可以选择基于任务的异步或操作

有没有在一个任务包的异步调用一个简单的方法?


解决方案

  

有没有在一个任务包的异步调用一个简单的方法?


举例 WebClient.DownloadStringCompleted

 公共静态类WebClientAsyncExtensions
{
    公共静态任务<串GT; DownloadStringTask(这WebClient的客户,URI地址)
    {
        VAR TCS =新TaskCompletionSource<串GT;();        DownloadStringCompletedEventHandler处理器= NULL;
        处理器=(发件人,E)=>
            {
                client.DownloadStringCompleted - =处理程序;                如果(e.Error!= NULL)
                {
                    tcs.SetException(e.Error);
                }
                其他
                {
                    tcs.SetResult(e.Result);
                }
            };        client.DownloadStringCompleted + =处理程序;
        client.DownloadStringAsync(地址);        返回tcs.Task;
    }
}

用法:

 异步无效DownloadExample()
{
    Web客户端的客户端=新的WebClient();
    等待client.DownloadStringTask(新的URI(HTTP:// HTTP://stackoverflow.com/questions/13266079/));
}

So far it seems that importing a service reference in VS2012 with "generate task-based operations" is not working. It os greyed out.

A test with a new project for WPF is working fine - I could select either task-based or async operations.

Is there a simple way on wrapping the async call in a task?

解决方案

Is there a simple way on wrapping the async call in a task?

Example for WebClient.DownloadStringCompleted

public static class WebClientAsyncExtensions
{
    public static Task<string> DownloadStringTask(this WebClient client, Uri address)
    {
        var tcs = new TaskCompletionSource<string>();

        DownloadStringCompletedEventHandler handler = null;
        handler = (sender, e) =>
            {
                client.DownloadStringCompleted -= handler;

                if (e.Error != null)
                {
                    tcs.SetException(e.Error);
                }
                else
                {
                    tcs.SetResult(e.Result);
                }
            };

        client.DownloadStringCompleted += handler;
        client.DownloadStringAsync(address);

        return tcs.Task;
    }
}

Usage:

async void DownloadExample()
{
    WebClient client = new WebClient();
    await client.DownloadStringTask(new Uri("http://http://stackoverflow.com/questions/13266079/"));
}

这篇关于WP8 SDK服务进口与参考不能基于任务的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆