Version 1.4 of Hangover Helper has a tiny twitter feed build in. The apps purpose is to give users 5 minutes of fun; it’s not a serious app. I don’t want to force users to log on to anything in the app, but I do want to show the user that he/she is not alone. Others have hangovers and to prove it, I will show some recent tweets from others with hangovers.

This turns out to be very simple using TweetSharp:
Download TweetSharp from Codeplex http://tweetsharp.codeplex.com/ and reference the dll’s to your app.
Go to http://www.twitter.com and register an app. You will get a “Customer key” and a “Consumer secret”. You will need these two values to register your Twitter Service object and start a search like this:
TwitterService service = new TwitterService("your key", "Your secret");
Then you kick off your search with a search term (in my case the word “hangover”):
service.Search("hangover", ProcessIncommingSearch);
You need a method to handle your asynchronous search result. Add the tweets via the dispatcher to get the result back into the UI thread:
public void ProcessIncommingSearch(TwitterSearchResult searchResult, TwitterResponse response)
{
if (response.StatusCode == HttpStatusCode.OK)
{
foreach (var status in searchResult.Statuses)
{
TwitterStatus inline = status;
Tweet tweet = new Tweet(inline);
Dispatcher.BeginInvoke(() => tweets.Items.Add(tweet));
}
}
}
}
The tweet object is holding the information regarding individual tweets:
public class Tweet
{
private TwitterStatus _status;
public Tweet(TwitterStatus status)
{
_status = status;
}
public string CreatedDate
{
get { return _status.CreatedDate.ToLongDateString() + " " + _status.CreatedDate.ToLocalTime().ToLongTimeString(); }
}
public string Text
{
get
{
return _status.Text;
}
}
public string ScreenName
{
get
{
return _status.User.ScreenName;
}
}
public string ProfileImageUrl
{
get
{
return _status.User.ProfileImageUrl;
}
}
}
In the xaml page, a listbox is displaying the tweet collection via a DataTemplate that formats the individual tweets. The image will download from the URL provided by twitter:
<ListBox Name="tweets" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="2,10,2,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Image VerticalAlignment="Top" Margin="0,10,10,0" Grid.RowSpan="3" Grid.Column="0" Grid.Row="0" Source="{Binding ProfileImageUrl}" Width="73" Height="73"></Image>
<StackPanel Grid.Column="1" Orientation="Vertical">
<TextBlock Text="{Binding ScreenName}" FontWeight="Bold" FontSize="20" Foreground="Black"></TextBlock>
<TextBlock Text="{Binding Text}" TextWrapping="Wrap" FontSize="18" VerticalAlignment="Top"></TextBlock>
<TextBlock Text="{Binding CreatedDate}" FontSize="14" Foreground="LightGray"></TextBlock>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Tags: twitter