Tag Archives: Windows Phone

Windows Phone twitter feed

First thing you’ll want to do is make an empty windows phone form.
Add a “Loaded” event to it, with the following code.

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            WebClient twitter = new WebClient();

            twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadStringCompleted);
            twitter.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=NASA"));

        }

First we make a new webclient, an object we will use to then download the information from the provided Uri. You can alter anything that comes after “?screen_name=” to whatever you want, the name you provide is the username from who you will download his twitter information.
Once we have fetched the information, it will come to us in an XML format. We’ll use Linq to XML to access this data and place it in our application.
First we need a reference.

And we add “using System.Xml.Linq; at the top.
Now we will make a small “help” class, where the XML information will be fetched from later in the code.

        public class TwitterItem
        {
            public string UserName { get; set; }
            public string Message { get; set; }
            public string ImageSource { get; set; }
        }

Now we are all set up for the code where we will load the feeds into our listbox using a Linq statement.

        void twitter_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
         

            if(e.Error != null)
                return;


              XElement xmlTweets = XElement.Parse(e.Result);

                listBox1.ItemsSource = from tweet in xmlTweets.Descendants("status")
                                       select new TwitterItem
                                       {
                                           ImageSource = tweet.Element("user").Element("profile_image_url").Value,
                                           Message = tweet.Element("text").Value,
                                           UserName = tweet.Element("user").Element("screen_name").Value
                                       };
                  }

There we go, that’s all the code we need to load a twitter feed into our application.
All that’s left is adding a listbox to our form.

          <ListBox Height="607" Margin="12,0,0,0" Name="listBox1" VerticalAlignment="Top">


                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="132">
                            <Image Source="{Binding ImageSource}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"/>
                            <StackPanel Width="370">
                                <TextBlock Text="{Binding UserName}" Foreground="#FFC8AB14" FontSize="28" />
                                <TextBlock Text="{Binding Message}" TextWrapping="Wrap" FontSize="24" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>


            </ListBox>