【问题标题】:Silverlight: Difficulty with ListBox and {Binding} syntaxSilverlight:ListBox 和 {Binding} 语法的困难
【发布时间】:2010-08-31 22:39:25
【问题描述】:

我正在构建一个测试 Windows Phone 7 Silverlight 应用程序。 (我一直在关注这个tutorial。)我在将列表项绑定到项属性时遇到问题。

获取输入用户名的推文:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        WebClient twitter = new WebClient();
        twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadStringCompleted);
        twitter.DownloadStringAsync(new Uri(String.Format("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name={0}", username.Text)));
    }

将这些推文添加到列表框:

    struct TwitterItem
    {
        public string UserName { get; set; }
        public string Message { get; set; }
        public string ImageSource { get; set; }
    }

    void twitter_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            return;
        }

        XElement xmlTweets = XElement.Parse(e.Result);

        IEnumerable<TwitterItem> tweetItems = from tweet in xmlTweets.Descendants("status")
                               select new TwitterItem   
                               {
                                   ImageSource = tweet.Element("user").Element("profile_image_url").Value,
                                   Message = tweet.Element("text").Value,
                                   UserName = tweet.Element("user").Element("screen_name").Value
                               };

        listBox1.ItemsSource = tweetItems;

        PageTitle.Text = tweetItems.First().UserName;
    }

PageTitle.Text 表明推文被正确解析......但它们没有被正确显示。

这是我尝试使用的列表框:

        <ListBox Height="454" Width="418" HorizontalAlignment="Left" Margin="36,128,0,0" Name="listBox1" VerticalAlignment="Top">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Height="132">
                        <Image Source="{Binding ImageSource}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0" />
                        <StackPanel Width="370">
                            <TextBlock Text="{Binding UserName}" Foreground="BlanchedAlmond" FontSize="28"/>
                            <TextBlock Text="{Binding Message}" TextWrapping="Wrap" FontSize="24"/>
                            <TextBlock Text="shows up just fine" TextWrapping="Wrap" FontSize="24"/>
                        </StackPanel>

                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

第三个TextBlock 显示得很好,所以这不是高度或宽度为 0 的问题。我怀疑问题出在Text={Binding Property} 上。还是我做错了什么?

【问题讨论】:

  • 在 WPF 中对我来说工作得很好...我看不出它在 Silverlight 中不应该工作的任何原因。如果出现绑定错误,您是否检查了 VS 中的输出窗口?此外,尝试使 TwitterItem 成为一个类而不是一个结构。我不确定它有什么影响,但尝试一下也没有什么坏处……
  • @Thomas Levesque 是的,存在绑定错误。我不知道看那里,谢谢。
  • 您应该将您的解决方案添加为答案,然后将其标记为已接受,以便人们知道:)

标签: c# silverlight windows-phone-7


【解决方案1】:

我将 TwitterItem 定义为 MainPage 的内部类,而它需要成为顶级类。

【讨论】:

    【解决方案2】:

    您需要将 ObservableCollection 传递给 listBox1.ItemsSource。然后 UI 会在你设置时更新。

    ObservableCollection<TwitterItem> o = new ObservableCollection<TwitterItem>();
    foreach(TwitterItem t in tweetItems)
    {
       o.Add(t);
    }
    listBox1.ItemsSource = o;
    

    我相信我在我的应用中遇到了同样的问题,这解决了它。

    【讨论】:

    • 你真的读过这个问题吗?一个。问题不在于列表没有更新;湾。 OP 已经解决了他的问题
    • 没有看到问题的最后一行。
    • 我发誓我确实读过它。我完全理解错了:)
    猜你喜欢
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多