Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is several way for you to do that:</p> <p>-The first way is for you to use the Microsoft.Bcl.Async nuget package, this package enable you to use async/await in a windows phone 7 application and will enable you to "just get the return the value" by awaiting sampleRequest in a similar way as you would be doing it if it was not asynchronous:</p> <pre><code> private async Task&lt;WebResponse&gt; sampleRequest() { try { HttpWebRequest httpLoginRequest = (HttpWebRequest)WebRequest.Create(new Uri(DisplayMessage.Authentication_URL)); httpLoginRequest.Method = DisplayMessage.Get_Method; Parameters = new Dictionary&lt;string, string&gt;(); WebResponse webResponse =await httpLoginRequest.GetResponseAsync(); } catch (Exception ex) { throw ex; } } </code></pre> <p>That can be used in classB like this:</p> <pre><code> WebResponse response=await classAInstance.sampleRequest() </code></pre> <p>-the second way is to pass your own callback as parameter of sampleRequest:</p> <pre><code> public class State { public HttpWebRequest WebRequest { get; set; } public Action&lt;HttpWebResponse &gt; ResponseCallBack; } void sampleRequest(Action&lt;HttpWebResponse &gt; responseCallBack) { HttpWebRequest httpLoginRequest = (HttpWebRequest)WebRequest.Create(new Uri(DisplayMessage.Authentication_URL)); //httpLoginRequest.Method = DisplayMessage.Get_Method; //Parameters = new Dictionary&lt;string, string&gt;(); httpLoginRequest.BeginGetResponse(new AsyncCallback(GetLoginCallback), new State(){WebRequest = httpLoginRequest, ResponseCallBack = responseCallBack }); } private void GetLoginCallback(IAsyncResult asynchronousResult) { try { State state= (State)asynchronousResult.AsyncState; HttpWebResponse httpresponse = (HttpWebResponse)state.WebRequest.EndGetResponse(asynchronousResult); ... //Dispatch request back to ui thread Deployment.Current.Dispatcher.BeginInvoke(() =&gt; { state.ResponseCallBack(httpresponse ); }); } catch(WebException ex) { } } </code></pre> <p>which you can use in classB like this:</p> <pre><code>classAInstance.sampleRequest((httpResponse)=&gt;{ //Do what you want with http response here }); </code></pre> <p>-the third way would be to just add an event on your classA that you will trigger in tGetLoginCallback and register for the event in classB:</p> <pre><code> public event EventHandler&lt;Response&gt; LoginComplete; void sampleRequest(){ //identical original one .... } private void GetLoginCallback(IAsyncResult asynchronousResult) { try { HttpWebRequest httpRequest = (HttpWebRequest)asynchronousResult.AsyncState; HttpWebResponse httpresponse = (HttpWebResponse)httpRequest.EndGetResponse(asynchronousResult); ... //Dispatch request back to ui thread Deployment.Current.Dispatcher.BeginInvoke(() =&gt; { if (LoginComplete != null) { LoginComplete(this,response); } }); } catch (WebException ex) { } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload