CarouselPage-滚动导致双滚动动画

时间:2019-05-31 09:04:09

标签: c# xamarin.forms carousel

我有一个轮播页面,其中包含3个项目的itemSource(我只选择3个项目,因为更多的项目开始使应用程序变慢)。更改页面后,我将当前选择的页面重置为轮播源中的中间项目,以便您始终可以向左/向右滚动。但是,这会引起一些问题,因为每次滚动时都会看到滚动,然后因为页面被重置为中间位置,因此页面又有另一个滚动回到中间位置。

将itemsource设置为每个可能的计费周期都会导致内存问题,因为我们可以谈论100多个计费周期。

所以我想将解决方案更改为:

  • 动态更新itemsource列表以将项目保持为仅3个,但仍允许我正确向左/向右滚动(如果我在OnCurrentPageChanged()中进行操作,则会导致出现错误,导致我无法更新项目来源)。
  • 删除第二个动画,以免将轮播页面重置为中心页面。

不幸的是,似乎没有关于此的任何文档,到目前为止我尝试过的所有方法都失败了:(

以下是我为此编写的当前代码:

public CustomerListPage()
{
    InitializeComponent();

    // By setting starting to true, the system won't try to the first carousel page when binding the ItemSource
    _isStarting = true;

    var dates = new List<DateTime> { DateTime.Now.AddMonths(-1), DateTime.Now, DateTime.Now.AddMonths(1) };
    ItemsSource = new ObservableCollection<CustomerListItemViewModel>(dates.Select(p => new CustomerListItemViewModel(Navigation, p)));

    _isStarting = false;
    CurrentPage = Children[1];
}

protected override void OnCurrentPageChanged()
{
    base.OnCurrentPageChanged();

    if (!_isStarting)
    {
        _isStarting = true;

        // Get the index of the current page
        CurrentIndex = Children.IndexOf(CurrentPage);
        var viewModel = (CustomerListItemViewModel)CurrentPage.BindingContext;
        var currentPageDate = viewModel.FirstOfMonth;

        // if CurrentIndex = 0, go back in time
        // if CurrentIndex = 2, go forwards in time
        if (CurrentIndex == 0)
        {
            // Check to see if the Customer has billing periods available for this
            var billingPeriods = BudgetHelper.GetBillablePeriods();

            if (billingPeriods.Contains(currentPageDate.ToString("yyyy-MM")))
            {
                var newViewModel = (CustomerListItemViewModel)Children[1].BindingContext;
                var nextViewModel = (CustomerListItemViewModel)Children[2].BindingContext;

                newViewModel.FirstOfMonth = currentPageDate;
                CurrentPage = Children[1];

                nextViewModel.FirstOfMonth = currentPageDate.AddMonths(1);
                viewModel.FirstOfMonth = currentPageDate.AddMonths(-1);
            }
            else
            {
                CurrentPage = Children[1];
            }
        }
        else if (CurrentIndex == 2)
        {
            // Check if we try to go into the future
            if (currentPageDate.ToString("yyyy-MM") == DateTime.Now.AddMonths(1).ToString("yyyy-MM"))
            {
                // Stay on the page that we are already on
                    CurrentPage = Children[1];
            }
            else
            {
                var prevViewModel = (CustomerListItemViewModel)Children[0].BindingContext;
                var newViewModel = (CustomerListItemViewModel)Children[1].BindingContext;

                newViewModel.FirstOfMonth = currentPageDate;
                CurrentPage = Children[1];

                prevViewModel.FirstOfMonth = currentPageDate.AddMonths(-1);
                viewModel.FirstOfMonth = currentPageDate.AddMonths(1);
            }
        }

        LoadCarouselPageData();
        _isStarting = false;
    }
}

0 个答案:

没有答案