我目前正在做一個小項目,它抓取一個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文檔。