Skip to content

Instantly share code, notes, and snippets.

@salvador-guerrero
Created September 17, 2018 19:07
Show Gist options
  • Save salvador-guerrero/a8332a917af40f944ed8c3feb70eaca3 to your computer and use it in GitHub Desktop.
Save salvador-guerrero/a8332a917af40f944ed8c3feb70eaca3 to your computer and use it in GitHub Desktop.
Autoscroll behavior for alexrainman/CarouselView
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;
}
}
}
}
}
@dalton5
Copy link

dalton5 commented Mar 22, 2020

Hi,

On the move carousel method I have the issue:

The specified child already has a parent. You must call removeView() on the child's parent first.'

How can I fix this?

@dalton5
Copy link

dalton5 commented Mar 22, 2020

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment