如何在WP7-8中实现可自动加载的listBox



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

我需要连接到web服务,并且来自xml解析。下面是一个使用Web服务的例子,如果有这样的请求http://beta.sztls.ru/mapp/eps/?count=10根据这样的请求,该服务使用最新的十条新闻构建xmlhttp://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=)

好的。如果我正确理解了所有内容,那么您希望从xml文件生成一个项集合。我想,对你来说,最好的选择是创建一个带有字段"Image"、"Title"、"Description"e.t.c.的实体类(你已经做过了),并在你的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();

现在您将拥有一个项目集合。你下一步想怎么处理它们?我想,创建一个UI,使用它。我想,会有一些控件,它们是使用这个集合生成的。因此,如果你想更新这个页面并添加一些新控件,你应该检查哪些控件已经添加到页面中。在这种情况下,我更喜欢在用户控件中创建一个字段,它会检查这一点。它应该是独一无二的,所以让我们把它称为"标题"。(在这些文档中,"title"似乎是唯一的)因此,当你想在页面上添加控件时,你应该检查是否没有一个控件的"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();

这应该行得通。

最新更新