【问题标题】:How to limit the contents of ListView Items in wp8.1 - Pagination Conceptwp8.1中如何限制ListView Items的内容 - 分页概念
【发布时间】:2015-04-22 19:59:00
【问题描述】:

我正在处理 WP8.1 项目我想在 ListBox 中显示内容。默认情况下,列表框将显示 5 个项目,当用户滚动到第 7 个项目时,它应该加载接下来的 10 个项目,循环应该继续直到显示所有数据(例如:就像我们滚动时 Excel 表中的滚动框大小如何最小化一样下)

【问题讨论】:

    标签: c# windows-phone-8 windows-phone-8.1 winrt-xaml


    【解决方案1】:

    为此,您需要两件重要的事情。

    1) 限制List(或任何Collection)中您将作为项目源分配给ListBox 的项目。

    2) 您必须检测到滚动到达列表框滚动查看器的末尾。

    现在,例如。你的XAML是这样的:

        <ListBox x:Name="lbx">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    

    这是后面的其余代码。

        // two lists are for assigning ItemsSource and
        // other is for all items to represent.
    
        List<string> lstInt = new List<string>();
        List<string> lstIntFull = new List<string>();
        void BlankPage3_Loaded(object sender, RoutedEventArgs e)
        {
            // below code will get ScrollViewer from ListBox and define the event handler to it's ViewChanged event.
    
            sv = VisualTreeHelper.GetChild((VisualTreeHelper.GetChild(lbx, 0) as Border), 0) as ScrollViewer;
            sv.ViewChanged += sv_ViewChanged;
    
            for (int i = 0; i < 100; i++)
            {
                lstIntFull.Add(i.ToString());
            }
    
            for (int i = 0; i <= 10; i++)
            {
                lstInt.Add(lstIntFull[i]);
            }
    
            lbx.ItemsSource = lstInt;
        }
    
        // event handler of ViewChanged event that we declared on PageLoad
        void sv_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
        {
            var verticalOffset = sv.VerticalOffset;
            var maxVerticalOffset = sv.ScrollableHeight; //sv.ExtentHeight - sv.ViewportHeight;
    
            if (maxVerticalOffset < 0 ||
                verticalOffset == maxVerticalOffset)
            {
    
                // Scrolled to bottom
                // So, write the code here to load next set of data
    
                var itemCount = (lbx.ItemsSource as List<string>).Count;
                for (int i = itemCount; i <= itemCount + 10; i++)
                {
                    if (i < 100)
                    {
                        lstInt.Add(lstIntFull[i]);
                    }
                }
    
                // await Task.Delay(2000);
    
                // lbx.ItemsSource = null;
                lbx.ItemsSource = lstInt;
            }
            else
            {
                // Not scrolled to bottom
    
            }
        }
    

    现在将您的示例与此示例进行比较。并按照步骤回答您的问题。

    这个概念被称为Pagination

    检查this link 以了解如何检测 ScrollViewer 到达可滚动高度的末尾。

    【讨论】:

      猜你喜欢
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      相关资源
      最近更新 更多