我需要从网站上获取数据并将这些数据放入我的Listbox
Xaml中。我正在使用Windows Phone。
这是我的c#功能:
private void DownLoadCompleted(object sender, HtmlDocumentLoadCompleted e)
{
ObservableCollection<PopularVideos> _popVideos = new ObservableCollection<PopularVideos>();
var data = e.Document.DocumentNode.SelectSingleNode("//div[@class='content']")
.Descendants("img")
.Select(img => new PopularVideos()
{
Titulo = img.Attributes["alt"].Value,
Url = img.Attributes["src"].Value,
}).ToList();
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
foreach (var item in data)
{
_popVideos.Add(new PopularVideos(item.Titulo, item.Url));
}
});
}
班级PopularVideos
:
public class PopularVideos
{
public PopularVideos() { }
public PopularVideos(string titulo, string url)
{
Titulo = url;
Url = url;
}
public string Titulo { get; set; }
public string Url { get; set; }
}
Xaml中的 Listbox
:
<ListBox Name="listBoxPopular" ItemsSource="{Binding Mode=OneWay}" DataContext="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Name="imagem" Source="{Binding Path=Url}"/>
<TextBlock Text="{Binding Path=Title}" Tap="HyperlinkButton_Tap" FontSize="30" Foreground="#FF159DDE" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
此代码未显示任何错误,但我的Listbox
仍然为空。
主页代码隐藏中的源代码:
namespace AppUnno
{
public partial class MainPage : PhoneApplicationPage
{
int aux = 3;
private List<Artista> _artistas;
public class MyDataContextClass
{
public ObservableCollection<PopularVideos> PopVideos;
public MyDataContextClass()
{
ObservableCollection<PopularVideos> PopVideos = new ObservableCollection<PopularVideos>();
}
private void DownLoadCompleted(object sender, HtmlDocumentLoadCompleted e)
{
PopVideos.Clear();
// Add to pop Videos.
}
}
// Constructor
public MainPage()
{
if (NetworkInterface.GetIsNetworkAvailable())
{
InitializeComponent();
ConsultaArtista("http://www.unnu.com/music-artists");
//ConsultaPopularVideos("http://www.unnu.com/popular-music-videos");
panorama.Visibility = Visibility.Collapsed;
// Set the data context of the listbox control to the sample data
DataContext = App.ViewModel;
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
carregaFeed();
ApplicationBarra(aux);
HtmlWeb.LoadAsync("http://www.unnu.com/popular-music-videos", DownLoadCompleted);
}
else
{
var result = MessageBox.Show("There are problems in your connection. Please verify your connection and try again!", "", MessageBoxButton.OK);
aux = 1;
ApplicationBarra(aux);
}
}
private void DownLoadCompleted(object sender, HtmlDocumentLoadCompleted e)
{
PopVideos.Clear();
var data = e.Document.DocumentNode.SelectSingleNode("//div[@class='content']")
.Descendants("img")
.Select(img => new PopularVideos()
{
Titulo = img.Attributes["alt"].Value,
Url = img.Attributes["src"].Value,
}).ToList();
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
foreach (var item in data)
{
PopVideos.Add(new PopularVideos(item.Titulo, item.Url));
}
});
}
错误是DownloadCompleted函数中的“PopVideos”:
*
“当前上下文中不存在'PopVideos'这个名称”
*
答案 0 :(得分:0)
您正在使用本地变量而不是属性:
ObservableCollection<PopularVideos> _popVideos = new ObservableCollection<PopularVideos>();
上面一行表示你正在使用一个局部变量,你对它做的任何更改都会在方法结束时消失。您可能想要使用您班级的属性,但没有。
你应该:
使用班级的属性。
是否公开(命名惯例也建议你称之为PopVideos)。
正确定义Xaml中的绑定以及DataContext。
保留Binded属性的相同实例,在构造函数导致绑定中断后执行PopVideos = new
之类的操作。
可能解决方案的代码示例:
由于缺少某些代码,因此应该是一个简单的示例:
<强> MyDataContextClass.cs:强>
public class MyDataContextClass{
public ObservableCollection<PopularVideo> PopVideos;
public MyDataContextClass(){
PopVideos = new ObservableCollection<PopularVideo>();
}
...
private void DownLoadCompleted(object sender, HtmlDocumentLoadCompleted e){
PopVideos.Clear();
// Add to pop Videos.
}
}
MyXamlViewClass.Xaml.cs:
它应该将 DataContext 设置为MyDataContextClass
的相关实例,这是一种方法:
public class MyXamlViewClass : UserControl{ // could be a window, or whatever you are using.
public MyXamlViewClass(MyDataContextClass vm){
DataContext = vm;
InitializeComponent();//If I remember the method name correctly
}
}
<强> MyXamlViewClass.Xaml:强>
确保正确绑定ObservableCollection
:
<ListBox Name="listBoxPopular" ItemsSource="{Binding PopularVideos Mode=OneWay}">
...
编辑(由于评论):
这应该是它的样子(不同的名字):
用此替换相关代码(确保正确更改):
MainPage.Xaml.cs:PasteBin
MainPage.Xaml:PasteBin