LongListSelector WP8 ItemRealized -infinite可滚动列表 -

时间:2013-08-21 08:17:24

标签: xaml windows-phone-8 infinite-scroll longlistselector

我正在尝试使用动态加载实现不定式可滚动List。 (如http://blogs.windows.com/windows_phone/b/wpdev/archive/2012/10/01/how-to-create-an-infinite-scrollable-list-with-longlistselector.aspx

  • DataSource绑定到ObservableCollection

  • 填写清单首先要将项目添加到集合

  • ItemRealizedEvent 开始进一步填充ObservableCollection

我认为ItemrealizedEvent是由滚动触发但是它总是在为每个项目的集合添加项目后触发。

- >所以它不是动态的,它只是加载一切

任何想法?

在PageClass中:

within the Constructor:
(...)
LLS_BooksListAll.DataContext = _viewModel.SearchAllViewModel;
LLS_BooksListAll.ItemsSource = _viewModel.SearchAllViewModel.MediumCollection;
(...)

private async void LLS_BooksListAll_ItemRealized(object sender, ItemRealizationEventArgs e)
{
    if ((LLS_BooksListAll.ItemsSource as ObservableCollection<Medium>) == null) return;

    //get number of loaded items
    int currentItemsCount = (LLS_BooksListAll.ItemsSource as ObservableCollection<Medium>).Count;

    if (!_viewModel.SearchAllViewModel.IsLoading && currentItemsCount >= _offsetKnob &&
        (e.Container.Content as Medium) != null)
    {
        if ((e.Container.Content as Medium).Equals((LLS_BooksListAll.ItemsSource as
            ObservableCollection<Medium>)[currentItemsCount - _offsetKnob]))
        {
            _pageNumberAll++;
            try
            {
                await _viewModel.SearchAllViewModel.SearchAll(TB_Search.Text, _pageNumberAll);
            }
            catch (RestException ex)
            {
                MessageBox.Show("Connection-Error:  LLS_BooksListAll_ItemRealized - " + ex.Message);
            }
        }
    }
}

在ViewModelClass中:

    public async void SearchAll(string searchword, int pageNumber)
    {
        if (pageNumber == 1) this.MediumCollection.Clear();
        IsLoading = true;
        SearchRequest search = new SearchRequest();
        String responseString = await search.Get(searchword, SearchRange.all, pageNumber);
        MediaUser response = JsonConvert.DeserializeObject<MediaUser>(responseString);
        foreach (Medium med in response.media)
        {
            MediumCollection.Add(med); //Filling the observable collection
        }
        IsLoading = false;            
    }      

1 个答案:

答案 0 :(得分:1)

ItemRealized有一个外部行为,但实际上它是有效的。它会在屏幕滚动之前预先加载一堆elementos,因此如果您看到10个元素,则ItemRealized预加载30或40并停止直到您滚动到预先加载更多项目,然后才能到达列表末尾。如果使用100个元素对其进行测试,则可以看到此行为。