【问题标题】:How to implement automatically loadable listBox in the WP7-8如何在 WP7-8 中实现自动加载列表框
【发布时间】:2012-11-20 10:53:29
【问题描述】:

请告诉我如何实现自动加载列表框。向下滚动时,应将元素添加到前一个元素。不幸的是我没有找到这样的列表的例子,我看到在网络中建议使用 ObservalableCollection,但我不明白如何向集合中添加元素。这是我的大量测试应用程序。

我需要连接到 web 服务并来自 xml 解析。这是一个使用 Web 服务的示例,如果像这样的请求 http://beta.sztls.ru/mapp/eps/?count=10 服务用最新的十个新闻构造 xml,在这样的请求 http://beta.sztls.ru/mapp/eps/?count=10&skip=10 上,它返回带有以下十个新闻的 xml 我有课

public class Item
    {
        public string Description { get; set; }
        public string Title { get; set; }
        public string Image { get; set; }
    }

在代码隐藏中

public partial class MainPage : PhoneApplicationPage
    {
        private ObservableCollection<Item> _collection;
        public ObservableCollection<Item> Collection
        {
            get
            {
                if (_collection == null)
                {
                    _collection = new ObservableCollection<Item>();
                }
                return _collection;
            }
        }

        private int endIndex = 0;

        // Конструктор
        public MainPage()
        {
            InitializeComponent();
            this._collection = new ObservableCollection<Item>();

            this.listBox1.ItemsSource = Collection;
            this.listBox1.Loaded += new RoutedEventHandler(listBox1_Loaded);

        }

        void listBox1_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                LoadItems(10);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.StackTrace);

            }
        }

        private void LoadItems(int count)
        {
            WebClient webClient = new WebClient();
            webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
            webClient.DownloadStringAsync(
                new Uri(String.Format("http://beta.sztls.ru/mapp/eps/" + "?count={0}" + "&skip={1}" + "&ticks={2}",
                                      count, this.endIndex, DateTime.Now.Ticks)));
                 this.endIndex+=count
        }

        void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                ParseResult(e.Result);
            }
        }

        private void ParseResult(string result)
        {
            XElement element = XElement.Parse(result);



            var res = from part in element.Descendants("news")
                      select new Item
                                 {
                                     Image = part.Element("image_url").Value,
                                     Title = part.Element("title").Value,
                                     Description = part.Element("description").Value
                                 };

           //Here,as far as I understand, I need like this Collection.Add(res)
        }
    }

在 Xaml 中

<toolkit:LongListSelector Grid.Row="1" IsFlatList="True" x:Name="listBox1">
                <toolkit:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                        <Image Source="{Binding Image}" Width="100" Height="100"/>
                            <StackPanel>
                                <TextBlock Text="{Binding Title}" FontSize="22" Foreground="Red"/>
                                <TextBlock Text="{Binding Decription}" FontSize="26" Foreground="Blue"/>
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>

                </toolkit:LongListSelector.ItemTemplate>
                <toolkit:LongListSelector.ListFooterTemplate>
                    <DataTemplate>
                        <TextBlock x:Name="footer"/>
                    </DataTemplate>
                </toolkit:LongListSelector.ListFooterTemplate>
            </toolkit:LongListSelector>

我如何跟踪我应该发送加载新项目的请求的时间?

提前感谢您的帮助,对我的英语感到抱歉。我用过http://www.bing.com/translator =)

【问题讨论】:

    标签: c# windows-phone-7 xaml data-binding


    【解决方案1】:

    好的。如果我正确理解了所有内容,您希望从 xml 文件生成项目集合。 我想,对你来说最好的选择是创建一个实体类,其中包含“图像”、“标题”、“描述”等字段(你已经完成了),并在 ParseResult() 方法中创建这个实体的集合.它看起来像:

    List<Entity> list = ParseResult(xdoc);
    

    在 ParseResult 中,您从 xml 文件中获取带有某种表达式的数据,例如:

    return (from node in xdoc.Descendants("something") select new Entity(node.Attribute("Title").Value, node.Attribute("Image").Value,... ).ToList();
    

    现在您将拥有一个项目集合。接下来你想对他们做什么?我想,创建一个用户界面,使用它。我想,会有一些控件是使用这个集合生成的。因此,如果您想更新此页面并添加一些新控件,您应该检查哪些已添加到页面中。在这种情况下,我宁愿在用户控件中创建一个字段,它会检查这一点。它应该是唯一的,所以让我们把它设为“标题”。(在这些文档中,“标题”似乎是唯一的)所以,当你想在你的页面上添加一个控件时,你应该检查是否没有一个控件具有相同的“title”作为你要添加的那个。

    希望,我理解的都是正确的。


    Alexandr,您只需要为您的项目实体创建一个构造函数,例如

    public Item(string desc, string title, string image)
            {
                this.Description = desc;
                this.Title = title;
                this.Image = image;
            }
    

    那么你可以更容易地填写列表:

    List<Item> list = (from node in xdoc.Descendants("news") select new Item(node.Element("description").Value, node.Element("title").Value, node.Element("image_url").Value)).ToList();
    

    应该可以的。

    【讨论】:

    • 是的,您理解正确。据我了解,我必须这样做List&lt;Item&gt; list = ParseResult(e.Result); listBox1.ItemsSource = list;private List&lt;Item&gt; ParseResult(string result) { XElement element = XElement.Parse(result); return (element.Descendants("news").Select(part =&gt; new Item { Image = part.Element("image_url").Value, Title = part.Element("title").Value, Description = part.Element("description").Value })).ToList(); }
    • 非常感谢您的帮助。我做了我想做的一切,最后一切都按原样进行。 =)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多