Tags: | Categories: Blog Posted by admin on 3/26/2010 10:57 AM | Comments (1)

Scott Guthrie started the thread of creating a Windows Phone 7 Twitter application in the Mix, followed by Miguel de Icaza creating an iPhone Twitter Client.

So I thought to extend the demo to a full Twitter client that can actually sends Tweets as well in Windows Phone 7. I created the UI for the twitter client to login and the Status Text Box.


I also wanted to use the location services, however the emulator does not support location just yet, always returns with data unavailable. Hopefully when the devices comes out, you’ll be able to use the GPS or the triangulation to get your location, therefore being able to send a tweet with a latitude and longitude.

The code is very simple to send a tweet, as in Silverlight we can use WebClient or HttpWebRequest, because we need to post and access to the header, we’ll use HttpWebRequest. As in Silverlight all calls are Async instead as like WPF that are sync, we’ll have to create AsyncCallBack methods to receive the information coming back from Twitter.

Finally we post the bytes with the status. Very simple and clean code.


Let’s looks the code:

public partial class SendTweet : UserControl
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");

        string _Message = "";
        public SendTweet()
        private void button1_Click(object sender, RoutedEventArgs e)
            // Send a tweet
            _Message = textBox1.Text;
            PostToTwitter(textBoxUsername.Text, passwordBox1.Password);

        private void PostToTwitter(string username, string password)
            // encode the username/password
            string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
            // determine what we want to upload as a status
            // set the method to POST
            request.Method = "POST";
            // set the authorization levels
            request.Headers["Authorization"] = "Basic " + user;
            request.ContentType = "application/x-www-form-urlencoded";
            // set up the stream
            request.BeginGetRequestStream(new AsyncCallback(RequestCallback), request);       


        private void RequestCallback(IAsyncResult asyncResult)
            UTF8Encoding encoding = new UTF8Encoding();

            Stream _body = request.EndGetRequestStream(asyncResult);

            byte[] formBytes = encoding.GetBytes("status=" + _Message);

            _body.Write(formBytes, 0, formBytes.Length);

            request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);


        private void ResponseCallback(IAsyncResult asyncResult)
            // If it fails, handler it better than I

Didn’t take much to do as I know Silverlight, this is the advantage for developers that want to create applications for Windows Phone 7 (WP7) no need to learn a new language like Objective-C as well as your libraries, besides UI, will work, you can just reference your dlls created in Silverlight 3 and 4 and use the functionality.

Hope this helps.



blog comments powered by Disqus