ช่วยด้วยโปรแกรม error

Last post 27 Aug 2010 12:09 by nantcom. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 26 Aug 2010 0:27

    • nstk
    • Top 100 Contributor
    • Joined on 24 Aug 2010
    • Posts 3

    ช่วยด้วยโปรแกรม error

    เป็นโปรแกรม 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>
  • 26 Aug 2010 9:45 In reply to

    Re: ช่วยด้วยโปรแกรม error

    แวะมาดันคับ ผมเองก็ไม่เคยลองเขียยน Connect twitter สะด้วยแหะ... แบบนี้ต้องให้คุณ pandula & คุณ nant มาชี้แจงสะหน่อยยแล้ววว
  • 27 Aug 2010 12:09 In reply to

    Re: ช่วยด้วยโปรแกรม error

    ก้อลอง clear .Items ทิ้งก่อน ตามที่มันบอกเลยครับ เหอๆ
Page 1 of 1 (3 items)