<分区>
只是想知道如何将其设置为 Xamarin CarouselViewControl?我设法在其他产品中找到它,但在 Xamarin 中找不到?
请帮忙。
<分区>
只是想知道如何将其设置为 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/