Tweet Search

Tweet Search is a Simple application to allow you to search for Tweets containing a particular query with LINQ and the Twitter® Search RSS Feed using Silverlight on Windows Phone 7.

Printer Friendly Download Tutorial (833KB) Download Source Code (17.1KB)

Step 1

Start Microsoft Visual Studio 2010 Express for Windows Phone, then Select File then New Project... Select "Visual C#" then "Silverlight for Windows Phone" and then "Windows Phone Application" from Templates, select a Location if you wish, then enter a name for the Project and then click OK, see below:

New Project

Step 2

A Windows Phone application Page named MainPage.xaml should then appear, see below:


Step 3

Select Project then Add Reference... The "Add Reference" window should appear, select "System.XML.Linq" from the ".NET" List, see below:

Add System.XML.Linq Reference

Step 4

Add the Reference to "System.XML.Linq" by Clicking on OK.
Select Project then "Add Class...", and select the "Class" Template is not already Selected, then change the "Name" to Tweet.cs see below:

Tweet Class

Step 5

Add the new Class to the Project by Clicking on Add, then in the Code View for the new Class, below the "{" of the line "public class Tweet" type the following:

private string _avatar;
private string _title;
private string _author;
private DateTime _published;

See Below:

Tweet Class Declarations

Step 6

While still in the Code View for Tweet.cs, below the "private DateTime _published;" line type the following Properties:

public string Avatar
  get { return _avatar; }
  set { _avatar = value; }

public string Title
  get { return _title; }
  set { _title = value; }

public string Author
  get { return _author; }
  set { _author = value; }

public DateTime Published
  get { return _published; }
  set { _published = value; }

See Below:

Tweet Class Properties

Step 7

Return to the MainPage Designer View by selecting the "MainPage.xaml" Tab.
Then in XAML Pane between the <Grid x:Name="ContentGrid" Grid.Row="1"> and </Grid> lines type the following XAML:

<Grid x:Name="ContentMain">
    <RowDefinition Height="80"/>
    <RowDefinition Height="*"/>
  <Grid x:Name="Toolbar" Grid.Row="0">
      <ColumnDefinition Width="*"/>
      <ColumnDefinition Width="Auto"/>
    <!-- Toolbar -->
  <!-- Content -->


MainPage XAML Pane


MainPage Design View

Step 8

Then from the Windows Phone Controls section in the Toolbox select the TextBox control:

TextBox Control

Step 9

Draw a TextBox onto the Toolbar Section (upper smaller section) of the Grid on the Page, below the Page Title, and in the XAML Pane below the <!-- Toolbar --> line, change "TextBox1" to the following:

<TextBox Grid.Column="0" Name="Subject"/> 

See below:

MainPage with Subject TextBox

Step 10

Then from the Windows Phone Controls section in the Toolbox select the Button control:

Button Control

Step 11

Draw a Button onto the Toolbar Section by dragging the Button from the Toolbox onto the Toolbar section of the Grid on the Page, then in the XAML Pane change the "Button1" line to the following:

<Button Grid.Column="1" Content="search" Click="Search_Click"/>

See below:

MainPage with Button

Step 12

While still in the XAML Pane for MainPage, below the <!-- Content--> line, type the following ListBox XAML:

<ListBox Grid.Row="1" Name="Results">
      <StackPanel Orientation="Horizontal">
        <Image Height="90" Width="90" VerticalAlignment="Top" Margin="5,5,5,5" Source="{Binding Path=Avatar}"/>
        <StackPanel Width="340">
          <TextBlock TextWrapping="Wrap" Text="{Binding Path=Title}"/>
          <TextBlock Text="{Binding Path=Published}"/>
          <TextBlock FontWeight="Bold" Text="{Binding Path=Author}"/>   

See below:

MainPage with ListBox

Step 13

While still in the Designer View for MainPage.xaml, Select the "page name" TextBlock (PageTitle) then goto the Properties Box and change the Text property to "tweet search" without the quotes, see below:

MainPage PageTitle Properties

Step 14

Right Click on the Page or the entry for "MainPage.xaml" in Solution Explorer and choose the "View Code" option. In the Code View above "namespace TweetSearch" type the following:

using System.Xml.Linq;

See Below:

MainPage Includes

Step 15

While still the Code View for MainPage.xaml.cs above "public MainPage()" type the following Methods:

private void Tweets(object Sender, DownloadStringCompletedEventArgs e)
  XElement _xml;
    if (!e.Cancelled)
      _xml = XElement.Parse(e.Result);
      foreach (XElement value in _xml.Elements("channel").Elements("item"))
        Tweet _tweet = new Tweet();
        XNamespace _google = "";
        _tweet.Avatar = value.Element(_google + "image_link").Value;
        _tweet.Title = value.Element("title").Value;
        _tweet.Published = DateTime.Parse(value.Element("pubDate").Value);
        _tweet.Author = value.Element("author").Value.Replace("", "");
    // Ignore Errors

private void Query(string Value)
  WebClient _client = new WebClient();
  _client.DownloadStringCompleted += Tweets;
  _client.DownloadStringAsync(new Uri(("" 
  + HttpUtility.UrlEncode(Value))));

See Below:

MainPage Tweets and Query Methods

Step 16

While still the Code View for MainPage.xaml.cs above "public MainPage()" type the following Event Handler:

private void Search_Click(object sender, RoutedEventArgs e)

See Below:

MainPage Event Handlers

Step 17

Save the Project as you have now finished the Windows Phone Silverlight application. Select the Windows Phone Emulator option then Select Debug then Start Debugging or click on Start Debugging:

Start Debugging

After you do, the following will appear in the Windows Phone Emulator after it has been loaded:

Application Running

Step 18

Tap the TextBox and then using the SIP or Keyboard enter something to Search for, then Tap on "search" to display the first fifty results, see below:

Tweet Search

Step 19

You can then Stop the application by selecting the Visual Studio 2010 application window and clicking on the Stop Debugging button:

Stop Debugging

This is a very simple example of getting search results from Twitter®, you can use this to search for hashtags or other text, you may also change it to show more than 50 results (change the rpp=50 value) or customise the interface - make it your own!


Creative Commons License

Copyright Comentsys © 2009 - 2010, All rights reserved. About | Contact | Link to Page
Valid XHTML 1.1! Valid CSS Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0 This website is ICRA rated Creative Commons License