2011-12-07 3 views
1

비동기 호출을 사용하여 웹 서비스에서 데이터를 가져 오는 페이지가 있습니다. 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"); 
       } 
      } 

문제 : 이 페이지는 (웹 서비스가있을 때 경우를 고려 웹 서비스에서 데이터를 가져 오는 시작로드 응답을하지 않고 컨트롤이 부분을 잡으려고합니다.) 다시 말하면 뒤로 버튼을 누르거나 페이지를 탐색하면 새 페이지에서 메시지 상자가 나타납니다.

어떻게 그럴 수 있습니다.

감사합니다 감사합니다

답변

0

마지막으로 그것을 해결하려고 추가 ifixed.

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/최고는 asyncState에있을 것입니다 어딘가에는 (검색 할 수 NavigationService.CurrentSource 속성 값을 저장 매개 변수,하지만 속성이 NavigationService.CurrentSource의 이전 및 새 값을 비교의 HandleForecastResponse에서뿐만 아니라

2 /를 작동 할 수 있습니다.이 방법을 사용하면 활성 페이지가 변경되거나하지 않은 경우 추론 할 수 있어야한다.

+0

이 솔루션도 사용할 수 있지만 바로 솔루션을 찾을 수 있습니다. – Mohit

0

에 의해 그 문제가

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