您好我有以下代码,我在Windows Phone 8中使用我的UI调用。
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;
}
我有这个代码在我的用户界面中运行 - &gt;
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
}
我希望从我的UI能够检查httpwebrequest任务何时完成,同时只显示progressindicator。
提前致谢。
答案 0 :(得分:1)
像这样重写GetToken:
public async Task GetToken()
{
string postData = "username=" + NTUser.username + "&appId=" + appId + "&signed=" + CreateSignedHex();
await post("authorize?", postData);
}
并将其称为:
await uC.GetToken();
// and now hide the progressindicator and proceed to the other page