开发者

Populating a listbox from an aysnc httpwebrequest

I'm currently doing a little project that grabs an XML document, parses it via Linq (picking certain elements) and then binds it to a listbox via an async httpwebrequest.

Here's the code;

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

Am i correct in using the dispatcher to talk to the UI thread in order to update the listbox? When executed the listbox gets nothing and i get the following output from 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.FileNot开发者_如何转开发FoundException' 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

I presume i'm not handling the threads correctly, but can't see where?

EDIT: i should note that when the debug writeline is uncommented it does output the full xml document correctly.


I suspect that the problem is with the closure around the linq statement.
You can't bind the ItemsSource this way.

I'd take the output from the linq statement and set that to a property and then update the actual itemssource on teh UI thread after you have the data.

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

System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => 
{ 
    lstbBouquets.ItemsSource = Bouquets;
}); 


  Search.Element("e2servicename")

may be null, or

  Search.Element("e2servicename").Value

may return a null. Explicit conversion operators (string or Nullable) are prefered over the .Value property to handle possible nullness.

You can read up more on this here.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜