0
我有一个WebClient,我用它来下载文件。
这是我的代码,其中我有一个ProgressDialog和Web客户端下载:取消WebClient DownloadTask异步
dialog = new ProgressDialog(mContext);
dialog.SetProgressStyle(Android.App.ProgressDialogStyle.Horizontal);
dialog.SetCancelable(true);
dialog.SetCanceledOnTouchOutside(true);
dialog.Show();// showing a dialog
string url = "myurl";
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
dialog.CancelEvent += (s, e) =>
{
webClient.CancelAsync();
//----------------------- Crashes Here
};
try
{
bytes = await webClient.DownloadDataTaskAsync(url);
}
catch (TaskCanceledException)
{
return;
}
catch (Exception a)
{
return;
}
如何取消下载在中间?
webClient.CancelAsync();
抛出异常:
Object reference not set to an instance of an object
你会得到什么异常,什么是堆栈跟踪? –
@StephenCleary“System.NullReferenceException:未将对象引用设置为对象的实例” – amitairos
请发布[足够的代码以重现问题](https://stackoverflow.com/help/mcve)。 –