c#链接在WINDOWS PHONE中列表框上包装的对象

本文关键字:包装 对象 列表 链接 WINDOWS PHONE | 更新日期: 2023-09-27 18:21:09

我有这段代码,正如你在屏幕打印中看到的那样,正确加载你想要的数据并存储对象列表PopulatVideos:

item    { Title = Hey Porsche, Url = http://www.unnu.com/wp-content/plugins/wordpress-popular-posts/timthumb.php?src=http://www.unnu.com/wp-content/uploads/2013/03/019.jpg&amp;h=65&amp;w=275 }    <>f__AnonymousType0<string,string>
item.Title  "Hey Porsche"   string
item.Url    "http://www.unnu.com/wp-content/plugins/wordpress-popular-posts/timthumb.php?src=http://www.unnu.com/wp-content/uploads/2013/03/019.jpg&amp;h=65&amp;w=275" string

需要在我的列表框中加载这些带有绑定的对象,否则也可以。但是windowsphone不适用于DataSource和DisplayMember。

我的XAML:

<ListBox Name="listBoxPopular">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <Image Name="imagem" Source="{Binding Path=Url}"/>
                            <TextBlock  Text="{Binding Titulo}" Tap="HyperlinkButton_Tap"  FontSize="30" Foreground="#FF159DDE" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </controls:PanoramaItem>

我的班级是:

class PopularVideos
    {
        public PopularVideos() { }
        public PopularVideos(string titulo, string url)
        {
            Titulo = titulo;            
            BitmapImage Img = new BitmapImage(new Uri(url));
        }
        public string Titulo { get; set; }
        public Uri Url { get; set; }
    }

我的代码指针是:

_popVideos = new List<PopularVideos>();
            var data = e.Document.DocumentNode.SelectSingleNode("//div[@class='content']")
               .Descendants("img")
               .Select(img => new
               {
                   Title = img.Attributes["alt"].Value,
                   Url = img.Attributes["src"].Value,
               }).ToList();
            foreach (var item in data)
            {
                PopularVideos pop = new PopularVideos(item.Title, item.Url);
                _popVideos.Add(new PopularVideos(item.Title, item.Url));
            }
            listBoxPopular.ItemsSource = _popVideos;

这段代码之所以有效,是因为它们携带了对象中的图像和链接,只是无法在我的列表框中显示。

c#链接在WINDOWS PHONE中列表框上包装的对象

  1. 有一个可绑定的ObservableCollection<Item>(最好在ViewModel中)
  2. 使用ListBox.ItemsSource属性绑定到所述ObservableCollection<Item>。适用具有约束力的标准规则
  3. ListBox中的每个项都将是绑定到控件的核心集合中的项的表示,因此绑定到其属性的方式与绑定到其他任何项的方式相同

回复评论:

好的,请再次阅读@Den发给你的文章。请将DataContext PropertyListBox中删除,将x:Name="myList"添加到ListBox中,并在后面的代码中:myList.DataContext = this;这不是最好的解决方案,但它很容易理解,首先您需要理解:)致以最良好的问候。