Updating the UI from a Background Thread : Thread « Data « Silverlight






Updating the UI from a Background Thread

Updating the UI from a Background Thread
   

<UserControl x:Class='SilverlightApplication3.MainPage'
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    xmlns:d='http://schemas.microsoft.com/expression/blend/2008' 
    xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' 
    mc:Ignorable='d' 
    d:DesignWidth='640' 
    d:DesignHeight='480'>
  <Grid x:Name="LayoutRoot" Background="White" Margin="6,6,6,6">
    <StackPanel>
      <Button Content="Retrieve XML and Load" Click="RetrieveXMLandLoad_Click"></Button>
      <ListBox x:Name="BooksListBox" Margin="4,4,4,4" Height="452"  >
        <ListBox.ItemTemplate>
          <DataTemplate>
            <StackPanel Margin="2,2,2,2">
              <TextBlock Text="{Binding Path=Author}" Margin="0,0,0,2"/>
              <TextBlock Text="{Binding Path=Title}" Margin="0,0,0,2"/>
              <TextBlock Width="550" Text="{Binding Path=Description}"  TextWrapping="Wrap" Margin="0,0,0,10"/>
            </StackPanel>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>
    </StackPanel>
  </Grid>
</UserControl>


//File: Page.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;

namespace SilverlightApplication3
{
  public partial class MainPage : UserControl
  {
    public MainPage()
    {
      InitializeComponent();
    }

    private void RetrieveXMLandLoad_Click(object sender, RoutedEventArgs e)
    {
      Uri location = new Uri("http://localhost:9090/Books.xml", UriKind.Absolute);
      WebRequest request = HttpWebRequest.Create(location);
      request.BeginGetResponse(new AsyncCallback(this.RetrieveXmlCompleted), request);
    }

    void RetrieveXmlCompleted(IAsyncResult ar)
    {
      List<Book> bookList;
      HttpWebRequest request = ar.AsyncState as HttpWebRequest;
      WebResponse response = request.EndGetResponse(ar);
      Stream responseStream = response.GetResponseStream();
      using (StreamReader streamreader = new StreamReader(responseStream))
      {
        XDocument xDoc = XDocument.Load(streamreader);
        bookList = (from b in xDoc.Descendants("Book")
         select new Book()
         {
           Author = b.Element("Author").Value,
           Title = b.Element("Title").Value,
           PublishedDate = Convert.ToDateTime(b.Element("DatePublished").Value),
           NumberOfPages = b.Element("NumPages").Value,
           ID = b.Element("ID").Value
         }).ToList();
      }
      Dispatcher.BeginInvoke(() => DataBindListBox(bookList));
    }

    void DataBindListBox(List<Book> list)
    {
      BooksListBox.ItemsSource = list;
    }
  }

  public class Book
  {
    public string Author { get; set; }
    public string Title { get; set; }
    public string ISBN { get; set; }
    public string Description { get; set; }
    public DateTime PublishedDate { get; set; }
    public string NumberOfPages { get; set; }
    public string Price { get; set; }
    public string ID { get; set; }
  }
}

   
    
    
  








Related examples in the same category

1.Implementing Threading in Silverlight ApplicationsImplementing Threading in Silverlight Applications
2.Implementing the DoWork eventImplementing the DoWork event
3.Reporting progress changes from the BackgroundWorkerReporting progress changes from the BackgroundWorker
4.Handling the completion of the background taskHandling the completion of the background task
5.Event Dispatcher TimerEvent Dispatcher Timer
6.No Background WorkerNo Background Worker
7.Canceling a BackgroundWorker when a user selects the Escape keyCanceling a BackgroundWorker when a user selects the Escape key
8.Event Dispatcher with ThreadEvent Dispatcher with Thread