简体   繁体   中英

Message Box popping on wrong page wp7

I have a page which is fetching data from a webservice using async call. If i get the response from webservice control goes to catch where a message box is pooped. The code is given below:

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);

This part is response

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");
                }
            }

Problem : when the page is loaded it starts fetching data from webservice(consider the case when the web service is not responding and control goes to catch part). In the mean time if we press the back button or navigate the page the message box popps on the new page.

How could i stop that.

Thanks and Regards

Haven't tested it, but it may work:

1/ Store the value of the NavigationService.CurrentSource property somewhere it can be retrieved (the best would be in the asyncState parameter, but a property may work as well

2/ In the HandleForecastResponse, compare the old and new value of the NavigationService.CurrentSource. This way, you should be able to deduce if the active page has changed or not.

ifixed that problem by add

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

try this

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");
    });
                    }
            }

Finally solved it.

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");
                    }
                });
            }

I just checked at the time of popping the message box the name of the Current UI application page. If its same as the page from which message box is initiated than it pops otherwise not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM