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

We will create a few classes now to help us with storing and retrieving user credentials, so that we don't ask for it every time we want to speak with Twitter for getting some information.

  1. Now the class to sorting out the credentials. We will have this class as a static so as to ensure one instance of the same. This class is mainly going to include a getter setter for username and password, a method to check if the user if logged in and another one to log out the user. You can get the code here.
  2. Now let us create another class to facilitate easy retrieval from twitter xml format results for any queries we make. This basically involves just creating a getter setter for all the values that you would like to retrieve from the xml document returned. You can get the format of the xml document from here. Here is what I've in my Status.cs data structure class.

    using System;

    using System.Net;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Documents;

    using System.Windows.Ink;

    using System.Windows.Input;

    using System.Windows.Media;

    using System.Windows.Media.Animation;

    using System.Windows.Shapes;


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




  3. Now let us looking into implementing the Login.xaml.cs, first thing here is if the user is already logged in, we need to redirect the user to the homepage, this we can accomplish using the event OnNavigatedTo, which is fired when the user navigates to this particular Login page. Here you utilize the navigate to method of NavigationService to goto a different page if the user is already logged in.

if (GlobalVariable.isLoggedin())

        this.NavigationService.Navigate(new Uri("/Home", UriKind.Relative));


  1. On the submit button click event, add the new event handler, which would save the perform the WebClient request and download the results as xml string.

WebRequest.RegisterPrefix("https://", System.Net.Browser.WebRequestCreator.ClientHttp);


The following line allows us to create a web client to create a web request to a url and get back the string response. Something that came as a great news with SL 4 for many SL developers.


WebClient myService = new WebClient();

myService.AllowReadStreamBuffering = true;

myService.UseDefaultCredentials = false;

myService.Credentials = new NetworkCredential(TwitterUsername.Text, TwitterPassword.Password);


Here in the following line, we add an event that has to be fired once the xml string has been downloaded. Here you can do all your XLINQ stuff.


myService.DownloadStringCompleted += new DownloadStringCompletedEventHandler(TimelineRequestCompleted);


myService.DownloadStringAsync(new Uri(""));


  1. Now let us look at implementing the TimelineRequestCompleted event. Here we are not actually using the string response we get from twitter, I just use it to ensure the user is authenticated successfully and then save the credentials and redirect to home page.

public void TimelineRequestCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)


if (e.Error != null)


MessageBox.Show("This application must be installed first");



If there is no error, we can save the credentials to reuse it later.




GlobalVariable.saveCredentials(TwitterUsername.Text, TwitterPassword.Password);

this.NavigationService.Navigate(new System.Uri("/Home", UriKind.Relative));



  1. Ok so now login page is done. Now the main thing – running this application. This credentials stuff would only work, if the application is run out of the browser. So we need fiddle with a few Silverlioght project settings to enable this. Here is how:



    Right click on Silverlight > properties then check the "Enable running application out of browser".



    Then click on Out-Of-Browser settings and check "Require elevated trust…" option. That's it, all done to run. Now press F5 to run the application, fix the errors if any. Then once the application opens up in browser with the login page, right click and choose install.


    Once you install, it would automatically run and you can login and can see that you are redirected to the Home page. Here are the files that are related to this posts. We will look at implementing the Home page, etc… in the next post. Please post your comments and feedbacks; it would greatly help me in improving my posts!


    Thanks for your time, catch you soon.

Posted on Saturday, March 6, 2010 1:08 PM | Back to top

Comments on this post: Silverlight 4 Twitter Client - Part 2

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Max | Powered by: