Created
September 17, 2018 19:07
-
-
Save salvador-guerrero/a8332a917af40f944ed8c3feb70eaca3 to your computer and use it in GitHub Desktop.
Autoscroll behavior for alexrainman/CarouselView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using CarouselView.FormsPlugin.Abstractions; | |
using Xamarin.Forms; | |
namespace YourNamespace.Behaviors | |
{ | |
/// <summary> | |
/// Scrolls to next carousel element automatically | |
/// </summary> | |
class AutoscrollCarouselBehavior : Behavior<CarouselView.FormsPlugin.Abstractions.CarouselViewControl> | |
{ | |
/// <summary> | |
/// Scroll delay in milliseconds | |
/// </summary> | |
public int Delay { get; set; } | |
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; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
On the move carousel method I have the issue:
How can I fix this?