เป็นโปรแกรม tweeter ค่ะ แต่พอรันแล้วเกิดปัญหา ว่า Items collection must be empty before using ItemsSource.ไม่รู้จะแก้ยังไงช่วยหน่อยนะค่ะ
cs
public class TwitterInfo
{
public string UserName { get; set; }
public string Message { get; set; }
public string ImageSouce { get; set; }
}
private void button1_Click(object sender, RoutedEventArgs e)
{
WebClient twitter = new WebClient();
twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler (twitter_downloadCompleted);
twitter.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" + textBox1.Text));
//listBox1.ItemsSource = null;
}
void twitter_downloadCompleted(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 TwitterInfo
{
ImageSouce = tweet.Element("user").Element("profile_image_url").Value,
Message = tweet.Element("text").Value,
UserName = tweet.Element("user").Element("screen_name").Value
};
}
xaml
<StackPanel Orientation="Horizontal" Height="132" >
<Image Source="{Binding Image}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"/>
<StackPanel Width="370">
<TextBlock Text="{Binding Username}" FontSize="28" Foreground="#FFC7C2C2" />
<TextBlock Text="{Binding Message}" FontSize="24" TextWrapping="Wrap" />
</StackPanel>
</StackPanel>
</ListBox>
<ListBox Height="520" HorizontalAlignment="Left" Margin="6,0,0,6" Name="listBox4"
VerticalAlignment="Bottom" Width="468">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left"
Name="stackPanel1" VerticalAlignment="Top"
Background="#FFCEC9C9" MinHeight="135">
<Image Source="{Binding ImageSource}" Height="75"/>
<StackPanel Width="365" Margin="10,0,0,0">
<TextBlock Name="textBlock3" Text="{Binding Username}" />
<TextBlock Name="textBlock2" Text="{Binding Message}" TextWrapping="Wrap" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>