Silverlight 4 Twitter Client – Part 5
- by Max
In this post, we will look at implementing the following in our twitter client - displaying the profile picture in the home page after logging in. So to accomplish this, we will need to do the following steps. 1) First of all, let us create another field in our Global variable static class to hold the profile image url. So just create a static string field in that. public static string profileImage { get; set; }
2) In the Login.xaml.cs file, before the line where we redirect to the Home page if the login was successful. Create another WebClient request to fetch details from this url - .xml">http://api.twitter.com/1/users/show/<TwitterUsername>.xml I am interested only in the “profile_image_url” node in this xml string returned. You can choose what ever field you need – the reference to the Twitter API Documentation is here.
3) This particular url does not require any authentication so no need of network credentials for this and also the useDefaultCredentials will be true. So this code would look like.
WebRequest.RegisterPrefix("http://", System.Net.Browser.WebRequestCreator.ClientHttp);
WebClient myService = new WebClient();
myService.AllowReadStreamBuffering = myService.UseDefaultCredentials = true;
myService.DownloadStringCompleted += new DownloadStringCompletedEventHandler(TimelineRequestCompleted_User);)
myService.DownloadStringAsync(new Uri("http://api.twitter.com/1/users/show/" + TwitterUsername.Text + ".xml"));
4) To parse the xml file returned by this and get the value of the “profile_image_url” node, the LINQ code will look something like
XDocument xdoc;
xdoc = XDocument.Parse(e.Result);
var answer = (from status in xdoc.Descendants("user")
select status.Element("profile_image_url").Value);
GlobalVariable.profileImage = answer.First().ToString();
5) After this, we can then redirect to Home page completing our login formalities.
6) In the home page, add a image tag and give it a name, something like
<Image Name="image1" Stretch="Fill" Margin="29,29,0,0" Height="73" VerticalAlignment="Top" HorizontalAlignment="Left" Width="73" />
7) In the OnNavigated to event in home page, we can set the image source url as below
image1.Source = new BitmapImage(new Uri(GlobalVariable.profileImage, UriKind.Absolute));
Hope this helps. If you have any queries / suggestions, please feel free to comment below or contact me.
Technorati Tags: Silverlight,LINQ,Twitter API,Twitter