當我在我的應用程序中加載頁面時,我發起了一系列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