Geeks With Blogs
Max's Blog .NET, Windows Phone, SQL Server and other exciting stuff

Finally Silverlight 4 RC is released and also that Windows 7 Phone Series will rely heavily on Silverlight platform for apps platform. its a really good news for Silverlight developers and designers. More information on this here. You can use SL 4 RC with VS 2010. SL 4 RC does not come with VS 2010, you need to download it separately and install it. So for the next part, be ready with VS 2010 and SL4 RC, we will start using them and not betas.

With this momentum, let us go to the next part of our twitter client tutorial. This tutorial will cover setting your status in Twitter and also retrieving your

1) As everything in Silverlight is asynchronous, we need to have some visual representation showing that something is going on in the background. So what I did was to create a progress bar with indeterminate animation. The XAML is here below.

<ProgressBar Maximum="100" Width="300" Height="50" Margin="20" Visibility="Collapsed" IsIndeterminate="True" Name="progressBar1" VerticalAlignment="Center" HorizontalAlignment="Center" />

2) I will be toggling this progress bar to show the background work. So I thought of writing this small method, which I use to toggle the visibility of this progress bar. Just pass a bool to this method and this will toggle it based on its current visibility status.

public void toggleProgressBar(bool Option)
    if (Option)
        if (progressBar1.Visibility == System.Windows.Visibility.Collapsed)
            progressBar1.Visibility = System.Windows.Visibility.Visible;
        if (progressBar1.Visibility == System.Windows.Visibility.Visible)
            progressBar1.Visibility = System.Windows.Visibility.Collapsed;

3) Now let us create a grid to hold a textbox and a update button. The XAML will look like something below

<Grid HorizontalAlignment="Center">
        <RowDefinition Height="50"></RowDefinition>
        <ColumnDefinition Width="400"></ColumnDefinition>
        <ColumnDefinition Width="200"></ColumnDefinition>
    <TextBox Name="TwitterStatus" Width="380" Height="50"></TextBox>
    <Button Name="UpdateStatus" Content="Update" Grid.Row="1" Grid.Column="2" Width="200" Height="50" Click="UpdateStatus_Click"></Button>

4) The click handler for this update button will be again using the Web Client to post values. Posting values using Web Client. The code is:

private void UpdateStatus_Click(object sender, RoutedEventArgs e)
    string statusupdate = "status=" + TwitterStatus.Text;
    WebRequest.RegisterPrefix("https://", System.Net.Browser.WebRequestCreator.ClientHttp);
    WebClient myService = new WebClient();
    myService.AllowReadStreamBuffering = true;
    myService.UseDefaultCredentials = false;
    myService.Credentials = new NetworkCredential(GlobalVariable.getUserName(), GlobalVariable.getPassword());
    myService.UploadStringCompleted += new UploadStringCompletedEventHandler(myService_UploadStringCompleted);
    myService.UploadStringAsync(new Uri(""), statusupdate);
    this.Dispatcher.BeginInvoke(() => ClearTextBoxValue());

5) In the above code, we have a event handler which will be fired on this request is completed – !! Remember SL is Asynch !! So in the myService_UploadStringCompleted, we will just toggle the progress bar and change some status text to say that its done. The code for this will be StatusMessage is just another textblock conveniently positioned in the page.

void myService_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
    if (e.Error != null)
        StatusMessage.Text = "Status Update Failed: " + e.Error.Message.ToString();

6) Now let us look at fetching the friends updates of the logged in user and displaying it in a datagrid. So just define a data grid and set its autogenerate columns as true.

7) Let us first create a data structure for use with fetching the friends timeline. The code is something like below:

namespace MaxTwitter.Classes
    public class Status
        public Status() {}
        public string ID { get; set; }
        public string Text { get; set; }
        public string Source { get; set; }
        public string UserID { get; set; }
        public string UserName { get; set; }

You can add as many fields as you want, for the list of fields, have a look at here. It will ask for your Twitter username and password, just provide them and this will display the xml file. Go through them pick and choose your desired fields and include in your Data Structure.

8) Now the web client request for this is similar to the one we saw in step 4. Just change the uri in the last but one step to

Be sure to change the event handler to something else and within that we will use XLINQ to fetch the required details for us. Now let us how this event handler fetches details.

public void parseXML(string text)
    XDocument xdoc;
    if(text.Length> 0)
        xdoc = XDocument.Parse(text);
    statusList = new List<Status>();
    statusList = (from status in xdoc.Descendants("status")
                  select new Status
                      ID = status.Element("id").Value,
                      Text = status.Element("text").Value,
                      Source = status.Element("source").Value,
                      UserID = status.Element("user").Element("id").Value,
                      UserName = status.Element("user").Element("screen_name").Value,
    //this.Dispatcher.BeginInvoke(() => CallDatabindMethod(StatusCollection));
    DataGridStatus.ItemsSource = statusList;
    StatusMessage.Text = "Datagrid refreshed.";

in the event handler, we call this method with e.Result.ToString() Parsing XML files using LINQ is super cool, I love it.



I am stopping it here for  this post. Will post the completed files in next post, as I’ve worked on a few more features in this page and don’t want to confuse you. See you soon in my next post where will play with Twitter lists. Have a nice day!

Posted on Tuesday, March 16, 2010 12:36 PM Silverlight | Back to top

Comments on this post: Silverlight 4 Twitter Client – Part 3

# re: Silverlight 4 Twitter Client – Part 3
Requesting Gravatar...
Its really an awesome code, gotta try it with VS 2010 beta
Left by Yoosuf on Mar 17, 2010 10:48 PM

# re: Silverlight 4 Twitter Client – Part 3
Requesting Gravatar...

I'd try make UpdateStause but i get an security exception.

Do you need to register the application in twitter?

Left by Oded on Apr 10, 2010 9:19 PM

Your comment:
 (will show your gravatar)

Copyright © Max | Powered by: