c# - Async/Await with RestSharp in Windows Phone 8 -
i did search on internet, there solutions windows phone 7 don't work windows phone 8 please me out i'm doing wrong. i'm writing wp8 app consumes api mashape. i'm using restsharp nuget package. here extension methods restsharp packages await.
public static class restclientextensions { public static task<irestresponse> executetaskasync(this restclient @this, restrequest request) { if (@this == null) throw new nullreferenceexception(); var tcs = new taskcompletionsource<irestresponse>(); @this.executeasync(request, (response) => { if (response.errorexception != null) tcs.trysetexception(response.errorexception); else tcs.trysetresult(response); }); return tcs.task; } public static task<t> executetaskasync<t>(this restclient @this, restrequest request) t : new() { if (@this == null) throw new nullreferenceexception(); var tcs = new taskcompletionsource<t>(); @this.executeasync<t>(request, (response) => { if (response.errorexception != null) tcs.trysetexception(response.errorexception); else tcs.trysetresult(response.data); }); return tcs.task; } }
here function in being called. function lies in api.cs class file.
public async task<sendmessage> sendmessage(string phone, string msg, textbox textbox) { // create post request required headers & parameters var request = new restrequest("sendsms/{api}.json", method.post); request.addurlsegment("api", apikey); request.addheader("x-mashape-key", "abcdefghixyz"); request.addparameter("msg", msg); request.addparameter("phone", phone); var temp = await client.executetaskasync<sendmessage>(request); return temp; }
here page1.xaml.cs file of needed.
private void appbarbutton_send_click(object sender, eventargs e) { api api = new api(); sendmessage = api.sendmessage(contactnumber, textbox_message.text, textbox_message).result; messagebox.show(a.message); }
if directly call restsharp .executeasync() method in sendmessage() method works fine. if way, executes till await condition, returns tcs.task , complete app halts. stays way , nothing changes. idea doing wrong?
i try , await return sendmessage
private async void appbarbutton_send_click(object sender, eventargs e) { api api = new api(); sendmessage = await api.sendmessage(contactnumber, textbox_message.text, textbox_message); messagebox.show(a.message); }
Comments
Post a Comment