7
  _fbClient.GetCompleted += new EventHandler<FacebookApiEventArgs>(OnFetchPageNotification);
  _fbClient.GetAsync(_kNotificationPath, new Dictionary<string, object> { { "access_token", _kPageAccessToken } });

How to convert above code into awaitable code in wp7:

 object = await _fbClient.GetAsync(_kNotificationPath, new Dictionary<string, object> { { "access_token", _kPageAccessToken } });

I have CTP Installed and task parallel library also.

4

1 回答 1

17

Async CTP 附带一个文档,该文档描述了如何使每个现有模式适应基于任务的异步模式。它说基于事件的事件更多变,但确实给出了一个例子:

public static Task<string> DownloadStringAsync(Uri url)
{
    var tcs = new TaskCompletionSource<string>();
    var wc = new WebClient();
    wc.DownloadStringCompleted += (s,e) =>
    {
        if (e.Error != null) tcs.TrySetException(e.Error);
        else if (e.Cancelled) tcs.TrySetCanceled();
        else tcs.TrySetResult(e.Result);
    };
    wc.DownloadStringAsync(url);
    return tcs.Task;
}

被包装的原始函数是DownloadStringAsync,参数与传递给此函数的参数匹配,并且DownloadStringCompleted是被监视的事件。


(相同的文档似乎可以在这里下载- 上面的示例(以及更多描述)来自“任务和基于事件的异步模式(EAP)”)

于 2012-10-12T07:03:03.023 回答