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

标签 xamarin xamarin.forms

<分区>

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

请帮忙。

最佳答案

您可以通过将 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>

AutoscrollCarouselBehavior.cs Reference

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;
              }
          }
      }
 }

这将自动滚动轮播页面,您可以根据需要设置Delay

关于Xamarin CarouselViewControl 支持自动滑动到下一个项目吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57785993/

相关文章:

android - MVVMCross Android 10.0 (Q) MainActivity 异常在 SetContentView() 上崩溃

c# - MSAL 未重定向到 Android 应用

xaml - Xamarin.Forms 需要 WrapLayout

xamarin - TypeConverter 在毛伊岛不可用?

c# - 防止用户返回上一个 Activity

c# - 如何在 monodroid 中删除用于编辑文本的 TextChangedListener

listview - Xamarin.Forms:软键盘与页面底部的最后一个条目(滚动时)重叠

c# - Xamarin-文件 :///android_asset

c# - MVVM 模式中的 Viewpager

c# - Datepicker 不会在 xamarin.forms 中的默认日期选择时触发 - Android