C# 如何在WP 7/8中使用RestClient将进度更改与异步等待一起使用?
我在RestClient中使用异步等待模式,以便从web服务下载数据。但是,当我需要在从webservice下载/上传数据时显示进度更改值时,我被卡住了 我在NavigatedTo上使用以下代码:C# 如何在WP 7/8中使用RestClient将进度更改与异步等待一起使用?,c#,web-services,windows-phone-7,windows-phone-8,asynchronous,C#,Web Services,Windows Phone 7,Windows Phone 8,Asynchronous,我在RestClient中使用异步等待模式,以便从web服务下载数据。但是,当我需要在从webservice下载/上传数据时显示进度更改值时,我被卡住了 我在NavigatedTo上使用以下代码: protected async override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedTo(e); textblock_R
protected async override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
textblock_Result.Text = await Call_LoginApi("pp@gmail.com", "1234567890") + "Done!";
}
并创建异步方法,如下所示:
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类不适用于我,并且不会在textblock_Progresschange上返回任何进度更改值
我经常上网,并关注类似的博客,如:[1]:
但还是忍不住。请帮我解决这个问题
此外,您可以在这里找到我的解决方案:[1]:
对于初学者来说,您尝试在可能无法访问UI的单独线程上更新UI。因此,即使进度发生了变化,也会起作用。它可能不起作用。您对不可用网络的错误处理是有问题的。万一发生错误,你什么也不做。下载StringTaskAsyncExprogress用于WebClient,而不是RestSharp。我找不到任何关于RestSharp支持进度的文档;你确定吗?@StephenCleary,不完全是我要找的。所以没有任何解决办法来执行我的任务?我已经附上了我的解决方案,请参阅。@user1784819:我从未使用过RestSharp。你为什么不问问他们的官方谷歌集团呢?