请告诉我如何实现自动加载列表框。向下滚动时,应将元素添加到前一个元素。不幸的是我没有找到这样的列表的例子,我看到在网络中建议使用 ObservalableCollection,但我不明白如何向集合中添加元素。这是我的大量测试应用程序。
我需要连接到 web 服务并来自 xml 解析。这里是一个使用 Web 服务的示例,如果像这样的请求http://beta.sztls.ru/mapp/eps/?count=10服务会在这样的请求http://beta上使用最新的十个新闻构造 xml 。 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 =)