I'm using the following code and it works nice
private void somethingButton_Click(object sender, System.Windows.RoutedEventArgs e) { WebClient webClient = new WebClient(); webClient.OpenReadCompleted += webClient_OpenReadCompleted; webClient.OpenReadAsync(new Uri(myUri)); } void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { if(e.Error != null) { messageTextBlock.Text = e.Error.Message; return; } using (Stream s=e.Result) { XDocument document = XDocument.Load(s); var q1 = from c in document.Descendants("result") select new IndeedResult { Title =((string)c.Element("title")).Trim(), ResultUri = ((string)c.Element("url")).Trim(), Date = ((string)c.Element("date")).Trim(), }; myDataGrid.ItemsSource = q1; } But I want to add another WebClient, webClient2 that will do exactly the same but has different uri and different structure so I will have webClient2_OpenReadCompleted...
The problem is that finally I need to merge (or do some logic before merge) var q1 from webClient_OpenReadCompleted and var q2 from webClient2_OpenReadCompleted and then
var mergedQs = q1.Union(q2).ToList(); myDataGrid.ItemsSource = mergedQs Is there a simple way how to do it? I don't know how to do it using those event handlers.
Answer: 1
Good question! Did it like this
void webClient_OpenReadCompleted( object sender, OpenReadCompletedEventArgs e ) { Stream stream = (Stream)e.Result; BinaryReader reader = new BinaryReader( stream ); byte[] buffer = reader.ReadBytes( (int)stream.Length ); Uri uri = (Uri)e.UserState; streams.Add( uri.AbsoluteUri, new MemoryStream( buffer ) ); } Note the usage of UserState in order to provide a unqiue key into my streams Dictionary. Works perfectly :-) This way you can work with as many files / images / binary data as you like!
See this to see just how powerful this can be....http://www.alansimes.com/warp3dsilverlighttestpage.html
by : Alan Simeshttp://stackoverflow.com/users/447501
No comments:
Post a Comment
Send us your comment related to the topic mentioned on the blog