Problem Binding Url to listBox.itemsSource with XElement
Im trying to make dinamically loading url in a list Box by Binding.
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 ContentPa开发者_C百科nel_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>
and the Xml source in the web
www.marca.com www.elmundo.com www.vidaextra.com
In my Windows Phone 7 emulator cant see anything, anyone can help me solve the problem?¿
You are binding the Listbox to an Enumerable<TwitterItem>
. As this doesn't implement INotifyPropertyChanged
the UI isn't notified when you update the ItemSource in the callback.
As an alternative, consider creating an "ObservableCollection<TwitterItem>
and binding to that. It automatically implements InotifyPropertyChanged
so you don't need to.
You may also want to reconsider putting a WebBrowser inside the DataTemplate. There will be a performance cost of using it and it's unlikely to produce a good user experience.
精彩评论