【问题标题】:c# link objects wrapped on the listbox in WINDOWS PHONEc# WINDOWS PHONE列表框上包裹的链接对象
【发布时间】:2013-07-04 21:13:04
【问题描述】:

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

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

需要将这些对象加载到我的列表框中用绑定否则也可以。但是windows phone 不支持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 xaml binding windows-phone


    【解决方案1】:
    1. 有一个可绑定的ObservableCollection&lt;Item&gt;(最好在 ViewModel 中)。
    2. 使用ListBox.ItemsSource 属性绑定到所述ObservableCollection&lt;Item&gt;。适用标准绑定规则。
    3. ListBox 中的每个项目都将代表绑定到控件的核心集合中的项目,因此绑定到其属性的方式与绑定到其他任何内容的方式相同。

    【讨论】:

    • 请记住,当您使用 ObservableCollection 时要更改它,您需要使用 Dispatcher 的实例。 (Deployment.Current.Dispatcher.BeginInvoke() method)
    • @DenDelimarsky 对不起,但我正在努力实现你所说的。一定是正确的,但我是初学者,我不知道该怎么做。
    • @MateuszDembski 我也不知道怎么用。帮帮我。
    • 我建议从数据绑定简介文章开始:codeproject.com/Articles/80555/…
    • @Luiz - StackOverflow 不是代码编写服务。您需要非常具体地说明什么不起作用,如果有人熟悉该主题,您将获得帮助。
    【解决方案2】:

    这是对评论的回复:

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

    【讨论】:

      猜你喜欢
      • 2012-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多