当我在我的应用程序中加载页面时,我发起了一系列WebClient.DownloadStringAsync
请求。这些出去执行json API调用。我注意到,如果用户在应用程序点击我的WebClient_StringCompleted
之前按下后退按钮,任务仍然完成,我宁愿不要。有没有办法使用OnBackKeyPress
覆盖来停止所有的异步任务?Windows Phone在按下按钮时取消异步任务
更新:
我最终混合了以下两个答案。这里是我的代码上解决:
WebClient VideoDetails = new WebClient();
id = parameter;
VideoDetails.DownloadStringCompleted += new DownloadStringCompletedEventHandler(VideoDetails_StringCompleted);
if (!cts.Token.IsCancellationRequested)
{
using (CancellationTokenRegistration ctr = cts.Token.Register(() => VideoDetails.CancelAsync()))
{
VideoDetails.DownloadStringAsync(new Uri("http://www.url.com/api/get_video_detail?id=" + VideoID));
}
}
CTS的类型CancellationTokenSource的类变量
然后我可以在我的OnBackKeyPress覆盖断火;
ct.Cancel();
这将使用Web客户端的CancelAsync()方法取消所有的WebClient的。
好吧,这样的视频最终是我采取的方法。 – 2013-05-14 01:37:12