Windows Phone 7 : Reading Twitter Timeline

Windows Phone 7 : Reading Twitter Timeline

  • Comments 2

This one is similar to my previous post on Reading RSS. But useful in Phone App.

So you need the structure as below

public class TWClass
{
    public string TextMSG { get; set; }
    public string CreatedDt { get; set; }
}

After that you need the below code to get your Twitter timeline. My Twitter handle is wriju_ghosh. I have hardcoded it. You can make it dynamic as well.

public Page1()
{
    InitializeComponent();
        
    WebClient myTweet = new WebClient();
    myTweet.DownloadStringCompleted += new DownloadStringCompletedEventHandler(myTweet_DownloadStringCompleted);

    myTweet.DownloadStringAsync(new Uri(@"http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=wriju_ghosh"));
}

void myTweet_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    //Need to check Network/WiFi etc before proceeding further..
    var TWData = from tw in XElement.Parse(e.Result).Descendants("status")
                    select new TWClass
                    {
                        TextMSG = tw.Element("text").Value,
                        CreatedDt = tw.Element("created_at").Value
                    };
    lstTW.ItemsSource = TWData;    
}

Now UI would look like (this can be better).

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <ListBox Name="lstTW" Margin="0,0,6,0">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Width="400">
                    <TextBlock Text="{Binding Path=TextMSG}"></TextBlock>
                    <TextBlock Text="{Binding Path=CreatedDt}"></TextBlock>
                    <TextBlock Text="----"></TextBlock>
                </StackPanel>                    
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>            
</Grid>

Namoskar!!!

Leave a Comment
  • Please add 6 and 1 and type the answer here:
  • Post
  • My Twitter handle is wriju_ghosh. I have hardcoded it. You can make it dynamic as well.

    what is hardcode? and how to do that? Sory i'm a newbie

  • This means I did not pass it through variable. If you pass it through variable then you can write a reusable function.

Page 1 of 1 (2 items)