Xamarin CarouselViewControl 支持自动滑动到下一个项目吗?

Wil*_*son 2 xamarin xamarin.forms

只是想知道如何将其设置为 Xamarin CarouselViewControl?我设法在其他产品中找到它,但在 Xamarin 中却找不到?

请帮忙。

Him*_*edi 5

Behaviour您可以通过附加到 CarouselViewControl来实现这一点

XAML:

xmlns:cv="clr-namespace:CarouselView.FormsPlugin.Abstractions;assembly=CarouselView.FormsPlugin.Abstractions"
xmlns:behaviour="clr-namespace:TestApp.Behaviours;assembly=TestApp"

<cv:CarouselViewControl x:Name="carousel"
                        ItemsSource="{Binding MySampleItems}"
                        ShowIndicators="true"
                        Orientation="Horizontal">

    <cv:CarouselViewControl.Behaviors>
         <behaviour:AutoscrollCarouselBehavior /> 
    </cv:CarouselViewControl.Behaviors>

    <cv:CarouselViewControl.ItemsSource>
       <!--Content of Carousel goes here-->
    </cv:CarouselViewControl.ItemsSource>

</cv:CarouselViewControl>
Run Code Online (Sandbox Code Playgroud)

AutoscrollCarouselBehavior.cs 参考

public class AutoscrollCarouselBehavior : Behavior<CarouselView.FormsPlugin.Abstractions.CarouselViewControl>
{
     /// <summary>
     /// Scroll delay in milliseconds
     /// </summary>
     public int Delay { get; set; } = 3000;

     private bool runTimer;
     private CarouselViewControl attachedCarousel;

     protected override void OnAttachedTo(CarouselViewControl bindable)
     {
        base.OnAttachedTo(bindable);
        runTimer = true;
        attachedCarousel = bindable;

        Device.StartTimer(TimeSpan.FromMilliseconds(Delay), () =>
        {
            MoveCarousel();
            return runTimer;
         });

      }

      protected override void OnDetachingFrom(CarouselViewControl bindable)
      {
          runTimer = false;
          base.OnDetachingFrom(bindable);
      }

      void MoveCarousel()
      {
         if (attachedCarousel.ItemsSource != null)
         {
             if (attachedCarousel.Position < attachedCarousel.ItemsSource.GetCount() - 1)
              {
                  attachedCarousel.Position++;
              }
              else
              {
                  attachedCarousel.Position = 0;
              }
          }
      }
 }
Run Code Online (Sandbox Code Playgroud)

这将自动滚动轮播页面,您可以Delay根据要求进行设置。