1

我正在尝试实现具有动态负载的不定式可滚动列表。(如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;            
    }      
4

1 回答 1

1

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

于 2013-08-21T08:31:39.003 回答