문제

Hi i have the following code, which I'm calling from my UI in windows phone 8.

    public void GetToken()
    {
        string postData = "username=" + NTUser.username + "&appId=" + appId + "&signed=" + CreateSignedHex();

        post("authorize?", postData);
    }

    private async Task<string> post(string url, string postdata)
    {
        var request = WebRequest.Create(new Uri(apiUrl + url)) as HttpWebRequest;
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        byte[] data = Encoding.UTF8.GetBytes(postdata);
        request.ContentLength = data.Length;

        using (var requestStream = await Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, request))
        {
            await requestStream.WriteAsync(data, 0, data.Length);
        }


        WebResponse responseObject = await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, request);
        var responseStream = responseObject.GetResponseStream();
        var sr = new StreamReader(responseStream);
        string received = await sr.ReadToEndAsync();

        return received;
    }

And i have this code to run in my UI ->

    private void Login_Click(object sender, RoutedEventArgs e)
    {


        ProgressIndicator progress = new ProgressIndicator
        {
            IsVisible = true,
            IsIndeterminate = true,
            Text = "Downloading details..."
        };
        SystemTray.SetProgressIndicator(this, progress);


        string userName = uiTextBoxUsername.Text;







        UserController uC = new UserController(userName, null);

        uC.GetToken();

       //HERE needs the logic which check whether the httpwebrequest task is completed
       //Proceed to another page
  }

I want from my UI to be able to check when the httpwebrequest task is completed, and in the meanwhile just show a progressindicator.

Thanks in advance.

도움이 되었습니까?

해결책

Rewrite GetToken like this:

public async Task GetToken()
{
    string postData = "username=" + NTUser.username + "&appId=" + appId + "&signed=" + CreateSignedHex();

    await post("authorize?", postData);
}

and call it like this:

await uC.GetToken();
// and now hide the progressindicator and proceed to the other page
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top