首页 > 解决方案 > Xamarin 表单集合视图分页

问题描述

我想以 xamarin 形式实现分页,实际上我还没有为任何移动应用程序这样做,所以这是我的第一次。我正在使用集合视图剩余项目阈值,并且正在触发事件,但未添加项目。我认为这是一个很好的逻辑,但我认为我错过了一些东西。提前致谢!

        private async void Init()
    {
        SetViews();

        Methods.SetFlowDirection(this);

        Methods.BeforeChecking(activityIndicator, parent);

        GetOrdersApiResponse response = await OrdersPageLogic.GetOrders();

        Methods.AfterChecking(activityIndicator, parent);
        //
        // get all orders
        orders = response.Orders;
        
        toRange = orders.Count >= pagination ? pagination : orders.Count;

        // only show first 10 items
        ordersToShow = orders.GetRange(0, toRange);

        // remove fetched items
        orders.RemoveRange(0, toRange);

        ordersCollView.ItemsSource = ordersToShow;
        
        ordersCollView.RemainingItemsThreshold = 2;

        ordersCollView.RemainingItemsThresholdReached += (s, e) => ordersCollView_RemainingItemsThresholdReached(s, e);
    }

    private void ordersCollView_RemainingItemsThresholdReached(object s, EventArgs e)
    {
        int count = 0;
        int to = 0;

        if (orders.Count == 0)
            return;

        to = pagination >= orders.Count ? orders.Count : pagination;

        foreach(var order in orders.GetRange(0, to))
        {
            if (count == pagination)
                break;

            ordersToShow.Add(order);
            count++;
        }

        // remove fetched items
        orders.RemoveRange(0, to);
    }

标签: c#xamarinxamarin.formsmobileuicollectionview

解决方案


因此,感谢@Jason,看来项目源(在我的情况下为 ordersToShow)必须是可观察的集合而不是列表


推荐阅读