2012-05-25 99 views
4
void GetResponseCallback(IAsyncResult asynchronousResult) 
     { 

      try 
      { 
       HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 
       HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); 
       Stream streamResponse = response.GetResponseStream(); 
       StreamReader streamRead = new StreamReader(streamResponse); 
       string responseString = streamRead.ReadToEnd(); 

       XmlReader xmlDoc = XmlReader.Create(new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(responseString))); 

       while (xmlDoc.Read()) 
       { 
        if (xmlDoc.NodeType == XmlNodeType.Element) 
        { 

         if (xmlDoc.Name.Equals("ResponseCode")) 
         { 
          responseCode = xmlDoc.ReadInnerXml(); 

         } 

        } 

       } 
       if (Convert.ToInt32(responseCode) == 200) 
       { 

        MessageBox.Show("Success"); 
       } 


       // Close the stream object 
       streamResponse.Close(); 
       streamRead.Close(); 
       // Release the HttpWebResponse 
       response.Close(); 


      } 
      catch (WebException e) 
      { 
       // Error treatment 
       // ... 
      } 
     } 

在上面的代碼中Messagebox.show dispalying「無效跨線程訪問」。請告訴我如何解決這個...在windows phone 7中無效的交叉線程訪問?

回答

8
Dispatcher.BeginInvoke(() => 
      MessageBox.Show("Your message") 
    ); 
6

與代碼UI進行的互動必須是調度線程,回調來自HTTP請求將不會在此線程上運行,因此出現錯誤。

您應該能夠使用像

Deployment.Current.Dispatcher.BeginInvoke(()=> MessageBox.SHow( 「成功」));

顯示消息框

HTH - Rupert。