问题使用XElement将URL绑定到listBox.itemsSource

时间:2011-05-28 10:55:23

标签: wpf windows-phone-7 linq-to-xml

我试图通过绑定在列表Box中进行dinamically加载URL。

Inicio.xaml.cs

void cardeek_DownloadUrlCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error != null) return;
    textBox1.Text = e.Result;
    XElement xmlUrl = XElement.Parse(e.Result);
    listBox1.ItemsSource = from url in xmlUrl.Descendants("user")
                           select new TwitterItem { Url = url.Element("card").Element("url").Value, };
}

private void ContentPanel_Loaded(object sender, RoutedEventArgs e)
{
    WebClient cardeekUrl = new WebClient();
    cardeekUrl.DownloadStringCompleted += new DownloadStringCompletedEventHandler(cardeek_DownloadUrlCompleted);
    cardeekUrl.DownloadStringAsync(new Uri("http://www.cardeek.com/wp7/response_url.php?email=" + "david.sonike@gmail.com" + "&code=" + "1"));
}

void cardeek_DownloadUrlCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error != null)
        return;
    textBox1.Text = e.Result;
    XElement xmlUrl = XElement.Parse(e.Result);
    listBox1.ItemsSource = from url in xmlUrl.Descendants("user")
                           select new TwitterItem
                           {
                               Url = url.Element("card").Element("url").Value,
                           };
}

private void ContentPanel_Loaded(object sender, RoutedEventArgs e)
{
    WebClient cardeekUrl = new WebClient();
    cardeekUrl.DownloadStringCompleted += new DownloadStringCompletedEventHandler(cardeek_DownloadUrlCompleted);
    cardeekUrl.DownloadStringAsync(new Uri("http://www.cardeek.com/wp7/response_url.php?email=" + "david.sonike@gmail.com" + "&code=" + "1"));
}

Inicio.xaml

<ListBox Height="416" HorizontalAlignment="Left" Margin="41,191,0,0" Name="listBox1" VerticalAlignment="Top" Width="367">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Background="{Binding coItemBackground}">
                <phone:WebBrowser HorizontalAlignment="Left" Margin="69,140,0,0" VerticalAlignment="Top" Height="121"
                        Width="137" Source="{Binding Url}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

和网络中的Xml源

www.marca.com www.elmundo.com www.vidaextra.com

在我的Windows Phone 7模拟器中看不到任何内容,任何人都可以帮我解决问题?¿

1 个答案:

答案 0 :(得分:1)

您将列表框绑定到Enumerable<TwitterItem>。由于这不实现INotifyPropertyChanged,因此在更新回调中的ItemSource时不会通知UI。

作为替代方案,请考虑创建一个“ObservableCollection<TwitterItem>并绑定到它。它会自动实现InotifyPropertyChanged,因此您不需要。

您可能还想重新考虑将WebBrowser放入DataTemplate中。使用它会产生性能成本,并且不太可能产生良好的用户体验。