Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 消息框弹出到错误的页面wp7_C#_Silverlight_Windows Phone 7_Messagebox - Fatal编程技术网

C# 消息框弹出到错误的页面wp7

C# 消息框弹出到错误的页面wp7,c#,silverlight,windows-phone-7,messagebox,C#,Silverlight,Windows Phone 7,Messagebox,我有一个使用异步调用从Web服务获取数据的页面。 如果我从webservice得到响应,控件将捕获消息框的弹出位置。 代码如下: string uri = "http://free.worldweatheronline.com/feed/weather.ashx?key=b7d3b5ed25080109113008&q=Mumbai&num_of_days=5"; UriBuilder fullUri = new UriBuilder("http://fr

我有一个使用异步调用从Web服务获取数据的页面。 如果我从webservice得到响应,控件将捕获消息框的弹出位置。 代码如下:

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");
                }
            }
问题: 当页面被加载时,它开始从webservice获取数据(考虑web服务没有响应并且控件转到catch部分的情况)。 同时,如果我们按下后退按钮或浏览页面,消息框将弹出新页面

我怎么能阻止它


感谢和问候

尚未测试它,但它可能会工作:

1/将NavigationService.CurrentSource属性的值存储在可以检索到的位置(最好是在asyncState参数中,但属性也可以工作)


2/在HandleForecastResponse中,比较NavigationService.CurrentSource的新旧值。这样,您应该能够推断活动页面是否已更改。

如果通过添加

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");
    });
                    }
            }
终于解决了

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应用程序页面的名称。如果它与启动消息框的页面相同,则它不会弹出。此解决方案也可以工作,但找到了一个直接的解决方案。