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)。 –