Convert an Event-Based Template for an Asynchronous CTP Template

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

How to convert the above code to the expected code in wp7:

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

I also have a CTP Installed and a parallel task library.

+4
source share
1 answer

Async CTP introduced a document that describes how to adapt each existing template to a task-based Async template. It says that an event-based event is more variable, but gives one example:

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

If the original function that wraps is DownloadStringAsync , the parameters correspond to the parameters passed to this function, and DownloadStringCompleted is the event that is being monitored.


(The same document looks downloadable here - the above example (and a more detailed description) refers to "Tasks and Asynchronous Events Based on Events" Template (EAP) ")

+15
source

Source: https://habr.com/ru/post/1439306/


All Articles