2012-02-28 79 views
1

希望對你們來說,一個簡單的問題,但我真的很掙扎。 我最近纔開始編程,剛剛有一個應用程序通過WP7應用程序商店認證,但注意到一個我自己想在公開應用程序之前修復的錯誤。取消webclient異步請求

基本上我有一個搜索框,用戶輸入化學名稱,web服務返回圖像及其分子量。我想要做的就是取消webclient,如果用戶在下載完成之前離開頁面,或者在前一個完成之前進行了新的搜索(由於我相信你只能有一個請求,所以現在崩潰了)在一個時間?)

private void searchCactus() 
     { 

      WebClient imgClient = new WebClient(); 
      imgClient.OpenReadCompleted += new OpenReadCompletedEventHandler(imgClient_OpenReadCompleted); 

      WebClient mwClient = new WebClient(); 
      mwClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(mwClient_DownloadStringCompleted); 

      if (DeviceNetworkInformation.IsNetworkAvailable == false) 
      { 
       MessageBox.Show("No network found, please check network availability and try again"); 
      } 
      else if (compoundSearchBox.Text.Contains("?")) 
       { 
        MessageBox.Show("\"?\" Not Permitted"); 
        return; 
       } 
      else if (compoundSearchBox.Text != "") 

       { 

        progBar1.IsIndeterminate = true; 

        string imageuri = "http://cactus.nci.nih.gov/chemical/structure/" + compoundSearchBox.Text + "/image?format=png&width=300&height=300"; 
        string mwURI = "http://cactus.nci.nih.gov/chemical/structure/" + compoundSearchBox.Text + "/mw"; 

        imgClient.OpenReadAsync(new Uri(@imageuri), imgClient); 



       mwClient.DownloadStringAsync(new Uri(@mwURI), mwClient); 

// //lower keyboard 
       this.Focus(); 

       } 
       else MessageBox.Show("Enter Search Query"); 

     } 

我試圖實現以下按鈕,但它不工作

private void buttonCancel_Click(object sender, RoutedEventArgs e) 
    { 
     imgClient.CancelAsync(); 
     mwClient.CancelAsync(); 
    } 

「名稱‘mwClient’不存在當前上下文存在」如果有人能提供,我將非常感激一些指導

+0

只需將兩個客戶端放入您班級的字段即可。 – svick 2012-02-28 20:26:48

+0

@svick謝謝,這比我預期容易,只是爲了澄清我添加公共部分類MWSearch:PhoneApplicationPage { 私人WebClient imgClient; 私人WebClient mwClient; ......那是你的意思?非常感謝 – shadyamigo 2012-02-28 20:58:15

回答

2

只需將這兩個客戶端放入您班級的字段即可。

+0

我在這裏看到的最短答案 – fnc12 2014-08-29 06:04:21