我正在使用与RestClient的Async Await模式,以便从Web服务下载数据。但是当我需要在从/向webservice下载/上传数据时显示Progress更改值时,我感到困惑。
我正在使用以下代码onnavigatedTo:
protected async override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
textblock_Result.Text = await Call_LoginApi("pp@gmail.com", "1234567890") + "Done!";
}
并将Async方法创建为:
private System.Threading.Tasks.Task<string> Call_LoginApi(string Email_Id, string pwd)
{
var progress = new Progress<DownloadStringTaskAsyncExProgress>();
progress.ProgressChanged += (s, e) =>
{
//MessageBox.Show(e.ProgressPercentage + "");
textblock_Progresschange.Text += e.ProgressPercentage + "";
};
var tcs = new System.Threading.Tasks.TaskCompletionSource<string>();
if (NetworkInterface.GetIsNetworkAvailable())
{
RestRequest parameters = new RestRequest("http://www.example.com/webservice/login.php", Method.POST);
parameters.AddParameter("email", Email_Id);
parameters.AddParameter("pwd", pwd);
//calling server with restClient
RestClient restClient = new RestClient();
restClient.ExecuteAsync(parameters,(response) =>
{
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
//upload successfull
//MessageBox.Show("Login completed succesfully...\n" + response.Content);
System.Diagnostics.Debug.WriteLine(response.Content + "");
tcs.SetResult(response.Content);
}
else
{
//error ocured during upload
//MessageBox.Show(response.StatusCode + "\n" + response.StatusDescription);
}
});
}
return tcs.Task;
}
public class DownloadStringTaskAsyncExProgress
{
public int ProgressPercentage { get; set; }
public string Text { get; set; }
}
其中Progress Class对我不起作用,并且不会在textblock_Progresschange上返回任何进度更改值。
我经常浏览并关注类似的博客:[1]:http://simplygenius.net/Article/AncillaryAsyncProgress
但无法帮助。请帮我解决这个问题。
此外,你可以在这里找到我的解决方案:[1]:http://1drv.ms/1ig3zch
此致