0

我需要从网站获取数据并将这些数据放入我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; }
    }

Listbox在 Xaml 中:

<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’”

*

4

1 回答 1

0

您正在使用局部变量而不是属性

    ObservableCollection<PopularVideos> _popVideos = new ObservableCollection<PopularVideos>();

上面的行表明您正在使用一个局部变量,您对它所做的任何更改都将在方法结束时随之消失。您可能想使用您班级的属性,但没有。

你应该:

  1. 使用你的类的属性。

  2. 让它公开(命名约定也建议您将其称为 PopVideos)。

  3. 正确定义 Xaml 中的绑定以及 DataContext。

  4. 保持 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}">
    ...

编辑(由于评论):

这就是它的外观(不同的名称):

.Xaml & .Xaml.cs

用这个替换你的相关代码(确保你正确地改变它):

MainPage.Xaml.cs:PasteBin

MainPage.Xaml:PasteBin

于 2013-07-10T22:10:19.683 回答