2012-03-16 80 views
0

我讀過通訊是異步WP7,但有些情況下不會缺少這種類型的通訊。WebClient等待操作 - Windows Phone 7

我正在使用webclient下載內容,只想在接收到此類內容後進入下一個操作。

這是如何完成的?

我是這個平臺上的noob。

Cumpz

回答

0

下面是一些代碼,這將有助於您瞭解如何使用WebClient類

創建Web客戶端

WebClient client = new WebClient()) 
       client.DownloadStringAsync(new Uri("http://www.google.com")); 
       client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted); 

現在開始,做一些事情一旦下載字符串操作完成

 void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
     string result = e.Result; 
     // Do something with the string 
     DoThingWithString(result) 
    } 
+1

WebClient不是一次性的。 – 2012-03-16 12:28:57

+1

它是,它從組件繼承(http://msdn.microsoft.com/en-us/library/system.componentmodel.component.aspx) 但是,在Windows Phone 7上下文中,它不會:) – ry8806 2012-03-16 13:48:13

1

這聽起來像你正在尋找同步ronous方法。如果是這樣的話,你可以這樣做:

 AutoResetEvent waitHandle = new AutoResetEvent(false); 
     WebRequest request = WebRequest.Create(url) as HttpWebRequest; 
     IAsyncResult asyncResult = request.BeginGetResponse(ar => waitHandle.Set(), null); 
     if (!waitHandle.WaitOne(30000)) 
     { 
      throw new TimeoutException("Timed out"); 
     } 
     using (HttpWebResponse response = request.EndGetResponse(asyncResult) as HttpWebResponse) 
     { 
      ...