2011-12-07 142 views
1

我有一個頁面,使用異步調用從web服務中獲取數據。 如果我從Web服務控制得到響應去捕捉消息框在哪裏。 的代碼如下:消息框彈出錯誤頁面wp7

string uri = "http://free.worldweatheronline.com/feed/weather.ashx?key=b7d3b5ed25080109113008&q=Mumbai&num_of_days=5"; 
      UriBuilder fullUri = new UriBuilder("http://free.worldweatheronline.com/feed/weather.ashx"); 
      fullUri.Query = "key=b7d3b5ed25080109113008&q=Mumbai&num_of_days=5"; 
      HttpWebRequest forecastRequest = (HttpWebRequest)WebRequest.Create(fullUri.Uri); 

      // set up the state object for the async request 
      ForecastUpdateState forecastState = new ForecastUpdateState(); 
      forecastState.AsyncRequest = forecastRequest; 

      // start the asynchronous request 
      forecastRequest.BeginGetResponse(new AsyncCallback(HandleForecastResponse), forecastState); 

這部分是響應

private void HandleForecastResponse(IAsyncResult asyncResult) 
      { 

       try 
       { 

       // get the state information 
       ForecastUpdateState forecastState = (ForecastUpdateState)asyncResult.AsyncState; 
       HttpWebRequest forecastRequest = (HttpWebRequest)forecastState.AsyncRequest; 

       // end the async request 
       forecastState.AsyncResponse = (HttpWebResponse)forecastRequest.EndGetResponse(asyncResult); 

       Stream streamResult; 
       string newCityName = ""; 
       //int newHeight = 0; 


       // get the stream containing the response from the async call 
       streamResult = forecastState.AsyncResponse.GetResponseStream(); 

       // load the XML 
       XElement xmlWeather = XElement.Load(streamResult); 

       } 
       catch (Exception ex) 
       { 
        MessageBox.Show("Connection Error"); 
       } 
      } 

問題: 當頁面被加載它開始從web服務獲取數據(考慮當web服務是沒有反應和控制去抓部分)。 與此同時,如果我們按下後退按鈕或導航頁面,則消息框會彈出新頁面。

我怎麼能阻止它。

感謝和問候

回答

0

終於解決了這個問題。

catch (Exception x) 
      { 
       Deployment.Current.Dispatcher.BeginInvoke(() => 
       { 
        var currentPage = ((App)Application.Current).RootFrame.Content as PhoneApplicationPage; 
        if ((currentPage.ToString()).Equals("MumbaiMarathon.Info.News")) 
        { 
         MessageBox.Show("Connection Error"); 
        } 
       }); 
      } 

我剛剛在彈出消息框時彈出了當前UI應用程序頁面的名稱。如果它與啓動消息框的頁面相同,則不會彈出。

0

沒有測試它,但它可能工作:

1 /存儲的地方,可以檢索(在NavigationService.CurrentSource屬性的值最好是在asyncState參數,但屬性也可以工作

2 /在HandleForecastResponse中,比較NavigationService.CurrentSource的舊值和新值。這樣,您應該能夠推斷活動頁面是否已更改。

+0

該解決方案也將工作,但找到了一個直接的解決方案。 – Mohit

0

ifixed了那個問題補充

System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => 
{ 
}); 

試試這個

private void HandleForecastResponse(IAsyncResult asyncResult) 
       { 

        try 
        { 

        // get the state information 
        ForecastUpdateState forecastState = (ForecastUpdateState)asyncResult.AsyncState; 
        HttpWebRequest forecastRequest = (HttpWebRequest)forecastState.AsyncRequest; 

        // end the async request 
        forecastState.AsyncResponse = (HttpWebResponse)forecastRequest.EndGetResponse(asyncResult); 

        Stream streamResult; 
        string newCityName = ""; 
        //int newHeight = 0; 


        // get the stream containing the response from the async call 
        streamResult = forecastState.AsyncResponse.GetResponseStream(); 

        // load the XML 
        XElement xmlWeather = XElement.Load(streamResult); 

        } 
        catch (Exception ex) 
        { 
    System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => 
              { 
         MessageBox.Show("Connection Error"); 
    }); 
        } 
      }