Skip to content

Instantly share code, notes, and snippets.

@davidtavarez
Created March 20, 2016 00:25
Show Gist options
  • Save davidtavarez/24c951287cdb319c1554 to your computer and use it in GitHub Desktop.
Save davidtavarez/24c951287cdb319c1554 to your computer and use it in GitHub Desktop.
Picture carousel for Android, iOS, WP using Xamarin.Forms. https://cdn.pbrd.co/images/2otpyQam.png
using System;
using Xamarin.Forms;
using System.Collections.ObjectModel;
namespace NAMESPACE.Controls
{
public class PicturesCarousel : ScrollView
{
AbsoluteLayout _ElementsStack = new AbsoluteLayout ();
public PicturesCarousel (string[] elements, double width, double height, int separator = 1)
{
Orientation = ScrollOrientation.Horizontal;
Content = this._ElementsStack;
if (!Equals (elements, null)) {
double x = 0f;
foreach (string picture in elements) {
var view = new Image () {
WidthRequest = width,
HeightRequest = height,
BackgroundColor = Color.FromRgb (245, 245, 245),
Source = picture,
Aspect = Aspect.AspectFill
};
AbsoluteLayout.SetLayoutFlags (view, AbsoluteLayoutFlags.None);
AbsoluteLayout.SetLayoutBounds (view, new Rectangle (x, 0, width, height));
this._ElementsStack.Children.Add (view);
x += width + separator;
}
this._ElementsStack.WidthRequest = x;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment