【发布时间】: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