Jan 8 2011

Using TweetSharp in a Windows Phone 7 app

Category: silverlightAdministrator @ 18:17

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:

Comments

1.
pingback topsy.com says:

Pingback from topsy.com

Twitter Trackbacks for
        
        Using TweetSharp in a Windows Phone 7 app
        [clr-namespace.com]
        on Topsy.com

2.
best mp3 player best mp3 player Nepal says:

It’s so nice Article. I appreciate it.

3.
casino en ligne casino en ligne India says:

I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful and beneficial to your readers Nice article.

4.
iphone 3g cases iphone 3g cases Nepal says:

great article. I want to see more article.

5.
headphones headphones Nepal says:

You had some nice points here. I done a research on the topic and got most peoples will agree with your blog.

6.
cell phone covers cell phone covers Nepal says:

Aw, this was a really great post. In theory I’d like to write like this also – taking time and real effort to make a good article… but what can I say… I procrastinate alot and never seem to get something done.

7.
noise cancelling headphones noise cancelling headphones Malta says:

I am going to save this article to share it with some of my friends.

8.
Create iPad application Create iPad application Russia says:

Thanks! And I wish you never to have hangover! ))

9.
flashlight flashlight Lithuania says:

Thanks for publishing about this. There’s a heap of good tech info on the internet. You’ve got a lot of that info here on your site. I’m impressed!

10.
flash drives flash drives Guatemala says:

So beautiful sharing!Thank you very much.

11.
lithium batteries lithium batteries Greenland says:

Good post,This was exactly what I needed to read today! I am sure this has relevance to many of us out there.

12.
SEO Outsourcing SEO Outsourcing United States says:

I wanted to thank you for this great read!! I definitely enjoyed every little bit of it. I have you bookmarked to check out new stuff on your post.

13.
pen drive pen drive Greenland says:

Cool, have not been able to find the information I was looking for before now, thanks to the blog owners for this master piece work…

14.
flashlight flashlight People's Republic of China says:

Good job! I think you have done ones research here.

15.
noise canceling headphones noise canceling headphones People's Republic of China says:

Hi guys, I apologise for the possible off-tobic but would anyone be able to recommend a good cheap web hosting company? I’m using a free one atm but it requires i have ads on my site which i don’t want. Thanks for any help.

16.
usb sticks usb sticks People's Republic of China says:

It's a nice place to share my thoughts. I'm willing to stick around here and watch it grow. As i also want to learn how to grow mine.

17.
iphone 3g cases iphone 3g cases People's Republic of China says:

It's a nice place to share my thoughts. I'm willing to stick around here and watch it grow.

18.
battery battery People's Republic of China says:

Great Article .This is a nice topic.

19.
usb flash drive usb flash drive People's Republic of China says:

Thank you for another great article.

20.
flash memory flash memory People's Republic of China says:

Thank You for This Blog,

21.
iphone cover iphone cover People's Republic of China says:

Thanks for sharing, please keep an update about this info.

22.
usb device usb device People's Republic of China says:

Thanks for sharing, i like it very much.

23.
pen drive pen drive People's Republic of China says:

i like this site too much. Good theme

24.
rechargeable batteries rechargeable batteries People's Republic of China says:

i like this site too much.

25.
flash drives flash drives People's Republic of China says:

Excellent tips ,keep this good working.

26.
lithium batteries lithium batteries People's Republic of China says:

Pretty good post. I just found your site and wanted to say that I have really enjoyed browsing your posts.In any case I'll be subscribing to your blog and I hope you post again soon

27.
battery battery People's Republic of China says:

Pretty good post. I just found your site and wanted to say that I have really enjoyed browsing your posts.

28.
noise cancelling headphones noise cancelling headphones People's Republic of China says:

Thanks for a great post and interesting comments.

29.
best mp3 player best mp3 player People's Republic of China says:

Pretty good post.keep this good working.i want to see you more articles.

30.
flash drives flash drives People's Republic of China says:

Your post is the best. I like it

31.
usb device usb device People's Republic of China says:

i like this artical very much thank you ,I want to see you more articles.

32.
speakers speakers People's Republic of China says:

i like this artical very much thank you,keep this good working.

33.
noise cancelling headphones noise cancelling headphones People's Republic of China says:

i like this artical very much thank you,keep this good working.and i want to see you more articles.

34.
iphone 3gs cases iphone 3gs cases People's Republic of China says:

Thanks for such a great post and the review, I am totally impressed!

35.
flashlights flashlights People's Republic of China says:

It’s really great post. I would like to appreciate your work .

36.
usb device usb device People's Republic of China says:

It’s really great post. I would like to appreciate your work .thank you again.

37.
torch torch People's Republic of China says:

It is my great pleasure to visit your website .

38.
usb sticks usb sticks People's Republic of China says:

Fantastic Read! Looking forward to more!

39.
electronics electronics People's Republic of China says:

never seen such thing before .i like it.

40.
torch torch People's Republic of China says:

It’s really great post. I would like to appreciate your work

41.
computer speakers computer speakers People's Republic of China says:

It’s really great post. I would like to appreciate your work,i like it.

42.
USb flash drive USb flash drive People's Republic of China says:

Thank you, great post !

43.
flash memory flash memory People's Republic of China says:

Thank you, great post ! keep this good working.

44.
pen drive pen drive People's Republic of China says:

The blog article very surprised to me! Your writing is good

45.
flashlight flashlight People's Republic of China says:

The blog article very surprised to me! Your writing is good,i want to see you more good artcles.

46.
flash memory flash memory People's Republic of China says:

You have a very good blog that the main thing a lot of  useful!

47.
speakers speakers People's Republic of China says:

You have a very good blog that the main thing a lot of  useful!i like it.

48.
flashlight flashlight People's Republic of China says:

This is a great post and makes me think of where I can fit in.

49.
pen drive pen drive People's Republic of China says:

This is a great post ,i want to see you more posts.

50.
torch torch People's Republic of China says:

Thanks for the article. Great tips.

51.
computer speakers computer speakers People's Republic of China says:

Thanks for the article. Great tips.keep this good working.

52.
electronics electronics People's Republic of China says:

I have read your article, I think this is very good!

53.
pen drive pen drive People's Republic of China says:

I have read your article, I think this is very good!keep this good working.

54.
Marketing Web 2.0 Marketing Web 2.0 United States says:

I really liked reading your post!. Quallity content. With such a valuable blog i believe you deserve to be ranking even higher in the search engines

55.
real estate vancouver island real estate vancouver island United States says:

Thanks for sharing this great article! That is very interesting I love reading and I am always searching for informative information like this.

56.
caloundra accommodation caloundra accommodation United States says:

I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!

57.
cheap aircards cheap aircards United States says:

I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful and beneficial to your readers Nice article.

58.
hesi test hesi test United States says:

I just stumbled upon this blog and I must say this is an exceptional post. I might just be checking in more regularly in future...............

59.
linux web hosting linux web hosting United States says:

Hi everybody else !!! The blog was totally fantastic! you all do such a fantastic job at such Concepts..., for one appreciate all you do!

60.
free sms free sms United States says:

This blog is a good one! Its original, thanks for the info! Is there a way I can tell my people about this post. If only we had more great blogs out there like this one!

61.
call to india call to india United States says:

Finally a blog post that is really worth reading. There are fantastic blog post which is share by you.

62.
mode schuhe mode schuhe says:

Ich mag lesen Ihre Post Sir. Ich fühle, dass ich ein Sachverständigen auf diesem Gebiet zu einem bestimmten Zeitpunkt zu werden. Vielen Dank für die mir die dringend benötigte Inspiration.

63.
Christian Louboutin Mary Janes Christian Louboutin Mary Janes People's Republic of China says:

Ich mag dieses Artikels, sind besonders gut.

64.
mode schuhe mode schuhe says:

<a href="www.schuhefeuer.org/...outin-Stiefel_6_1.htm" > christian louboutin decollete </a>
<a href="www.schuhefeuer.org/...tin-Slingback_5_1.htm" > christian louboutin shoes online </a>
Ich mag sehr viel Ihre Art der Präsentation. Wir freuen uns auf mehr lesen von dir.
<a href="www.schuhefeuer.org/...tin-Slingback_5_1.htm" > christian louboutin online kaufen </a>
<a href="www.schuhefeuer.org/...in-Mary-Janes_2_1.htm" > christian louboutin deutschland </a>

65.
Forklifts Spares Forklifts Spares United States says:

The post is written in very a good manner and it entails many useful information for me. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. Thank you for the post.