2011-10-13 75 views
0

我目前正在做一個小項目,它抓取一個XML文檔,通過Linq解析它(挑選某些元素),然後通過異步httpwebrequest將它綁定到一個列表框。從aysnc填充列表框httpwebrequest

這是代碼;

void ResponseCallBack(IAsyncResult result) 
    { 
     //get to the request object 
     HttpWebRequest myRequest = result.AsyncState as HttpWebRequest; 
     try 
     { 
      //need error checking 
      HttpWebResponse response = myRequest.EndGetResponse(result) 
       as HttpWebResponse; 
      using (StreamReader sr = new StreamReader(response.GetResponseStream())) 
      { 
       String s = sr.ReadToEnd(); 

       XElement xmlSearch = XElement.Parse(s); 
       System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => 
       { 

        lstbBouquets.ItemsSource = from Search in xmlSearch.Descendants("e2service") 
               select new GetBouquets 
               { 

                e2servicename = Search.Element("e2servicename").Value 
               }; 
       }); 

       //System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { Debug.WriteLine(s); }); 


       // Stop progress bar 
       System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { performanceProgressBar.IsIndeterminate = false; }); 

      } 
     } 
     catch (WebException webExcp) 
     { 
      //Debug only, needs error checking 
      System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { Debug.WriteLine(webExcp.ToString()); }); 
     } 
    } 

我是否正確使用調度程序與UI線程交談以更新列表框?當執行該列表框時什麼也得不到,我從VS獲得以下輸出;

A first chance exception of type 'System.MethodAccessException' occurred in mscorlib.dll 'UI Task' (Managed): Loaded 'System.SR.dll' A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll System.Windows.Data Error: Cannot get 'e2servicename' value (type 'System.String') from 'DreamboxRemote.Pages.GetBouquets' (type 'DreamboxRemote.Pages.GetBouquets'). BindingExpression: Path='e2servicename' DataItem='DreamboxRemote.Pages.GetBouquets' (HashCode=98879357); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String').. System.MethodAccessException: Attempt to access the method failed: DreamboxRemote.Pages.GetBouquets.get_e2servicename() at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark) at System.Reflection.RunA first chance exception of type 'System.MethodAccessException' occurred in mscorlib.dll

我認爲我沒有正確處理線程,但看不到在哪裏?

編輯:我應該注意,當調試writeline未註釋它確實輸出完整的XML文檔。

回答

2

我懷疑問題是圍繞linq語句的閉包。
你不能以這種方式綁定ItemsSource。

我會從linq語句中獲取輸出並將其設置爲屬性,然後在獲取數據後更新UI線程上的實際itemssource。

Bouquets = from Search in xmlSearch.Descendants("e2service") 
      select new GetBouquets 
      { 
       e2servicename = Search.Element("e2servicename").Value 
      }; 

System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => 
{ 
    lstbBouquets.ItemsSource = Bouquets; 
}); 
0
Search.Element("e2servicename") 

可以爲空,或者

Search.Element("e2servicename").Value 

可能會返回一個空。顯式轉換運算符(字符串或可空值)優先於.Value屬性來處理可能的空值。

您可以在此here瞭解更多。

e2servicename = (string) Search.Element("e2servicename")