2011-12-07 118 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"); 
    }); 
        } 
      }