WP8 SDK import Service Reference with task-based operations not possible

前端 未结 1 814
滥情空心
滥情空心 2021-01-14 19:49

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

相关标签:
1条回答
  • 2021-01-14 20:26

    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/"));
    }
    
    0 讨论(0)
提交回复
热议问题