Skip to content
This repository was archived by the owner on Jul 17, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
<AndroidUseLatestPlatformSdk>true</AndroidUseLatestPlatformSdk>
<TargetFrameworkVersion>v7.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v6.0</TargetFrameworkVersion>
<AndroidSupportedAbis>armeabi,armeabi-v7a,x86</AndroidSupportedAbis>
<AndroidStoreUncompressedFileExtensions />
<MandroidI18n />
Expand Down
118 changes: 56 additions & 62 deletions CarouselViewSample/CarouselViewSample/CarouselIndicators.cs
Original file line number Diff line number Diff line change
@@ -1,113 +1,109 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace CarouselViewSample
{
public class CarouselIndicators : Grid
{
private ImageSource UnselectedImageSource = null;
private ImageSource SelectedImageSource = null;
private readonly StackLayout _indicators = new StackLayout() { Orientation = StackOrientation.Horizontal, HorizontalOptions = LayoutOptions.CenterAndExpand };
ImageSource _unselectedImageSource;
ImageSource _selectedImageSource;
readonly StackLayout _indicators = new StackLayout { Orientation = StackOrientation.Horizontal, HorizontalOptions = LayoutOptions.CenterAndExpand };

public CarouselIndicators()
{
this.HorizontalOptions = LayoutOptions.CenterAndExpand;
this.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
this.Children.Add(_indicators);
}
public static readonly BindableProperty PositionProperty = BindableProperty.Create(nameof(Position), typeof(int), typeof(CarouselIndicators), 0, BindingMode.TwoWay,
propertyChanging: PositionChanging);

public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(nameof(ItemsSource), typeof(IEnumerable), typeof(CarouselIndicators),
Enumerable.Empty<object>(), BindingMode.OneWay, propertyChanged: ItemsChanged);

public static readonly BindableProperty SelectedIndicatorProperty = BindableProperty.Create(nameof(SelectedIndicator), typeof(string), typeof(CarouselIndicators), "");

public static readonly BindableProperty PositionProperty = BindableProperty.Create(nameof(Position), typeof(int), typeof(CarouselIndicators), 0, BindingMode.TwoWay, propertyChanging: PositionChanging);
public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(nameof(ItemsSource), typeof(IEnumerable), typeof(CarouselIndicators), Enumerable.Empty<object>(), BindingMode.OneWay, propertyChanged: ItemsChanged);
public static readonly BindableProperty SelectedIndicatorProperty = BindableProperty.Create(nameof(SelectedIndicator), typeof(string), typeof(CarouselIndicators), "", BindingMode.OneWay);
public static readonly BindableProperty UnselectedIndicatorProperty = BindableProperty.Create(nameof(UnselectedIndicator), typeof(string), typeof(CarouselIndicators), "", BindingMode.OneWay);
public static readonly BindableProperty IndicatorWidthProperty = BindableProperty.Create(nameof(IndicatorWidth), typeof(double), typeof(CarouselIndicators), 0.0, BindingMode.OneWay);
public static readonly BindableProperty IndicatorHeightProperty = BindableProperty.Create(nameof(IndicatorHeight), typeof(double), typeof(CarouselIndicators), 0.0, BindingMode.OneWay);
public static readonly BindableProperty UnselectedIndicatorProperty = BindableProperty.Create(nameof(UnselectedIndicator), typeof(string), typeof(CarouselIndicators), "");

public static readonly BindableProperty IndicatorWidthProperty = BindableProperty.Create(nameof(IndicatorWidth), typeof(double), typeof(CarouselIndicators), 0.0);

public static readonly BindableProperty IndicatorHeightProperty = BindableProperty.Create(nameof(IndicatorHeight), typeof(double), typeof(CarouselIndicators), 0.0);

public string SelectedIndicator
{
get { return (string)this.GetValue(SelectedIndicatorProperty); }
set { this.SetValue(SelectedIndicatorProperty, value); }
get { return (string)GetValue(SelectedIndicatorProperty); }
set { SetValue(SelectedIndicatorProperty, value); }
}

public string UnselectedIndicator
{
get { return (string)this.GetValue(UnselectedIndicatorProperty); }
set { this.SetValue(UnselectedIndicatorProperty, value); }
get { return (string)GetValue(UnselectedIndicatorProperty); }
set { SetValue(UnselectedIndicatorProperty, value); }
}

public double IndicatorWidth
{
get { return (double)this.GetValue(IndicatorWidthProperty); }
set { this.SetValue(IndicatorWidthProperty, value); }
get { return (double)GetValue(IndicatorWidthProperty); }
set { SetValue(IndicatorWidthProperty, value); }
}

public double IndicatorHeight
{
get { return (double)this.GetValue(IndicatorHeightProperty); }
set { this.SetValue(IndicatorHeightProperty, value); }
get { return (double)GetValue(IndicatorHeightProperty); }
set { SetValue(IndicatorHeightProperty, value); }
}

public int Position
{
get { return (int)this.GetValue(PositionProperty); }
set { this.SetValue(PositionProperty, value); }
get { return (int)GetValue(PositionProperty); }
set { SetValue(PositionProperty, value); }
}

public IEnumerable ItemsSource
{
get { return (IEnumerable)this.GetValue(ItemsSourceProperty); }
set { this.SetValue(ItemsSourceProperty, (object)value); }
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}

private void Clear()
int _currentlySelected;

void Clear()
{
_indicators.Children.Clear();
}

private void Init(int position)
public CarouselIndicators()
{
HorizontalOptions = LayoutOptions.CenterAndExpand;
RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
Children.Add(_indicators);
}

if (UnselectedImageSource == null)
UnselectedImageSource = ImageSource.FromFile(UnselectedIndicator);
void Init(int position)
{
if (_unselectedImageSource == null)
_unselectedImageSource = ImageSource.FromFile(UnselectedIndicator);

if (SelectedImageSource == null)
SelectedImageSource = ImageSource.FromFile(SelectedIndicator);
if (_selectedImageSource == null)
_selectedImageSource = ImageSource.FromFile(SelectedIndicator);

if (_indicators.Children.Count > 0)
{
for (int i = 0; i < _indicators.Children.Count; i++)
{
if (((Image)_indicators.Children[i]).ClassId == nameof(State.Selected) && i != position)
_indicators.Children[i] = BuildImage(State.Unselected, i);
else if (((Image)_indicators.Children[i]).ClassId == nameof(State.Unselected) && i == position)
_indicators.Children[i] = BuildImage(State.Selected, i);
}
((Image)_indicators.Children[_currentlySelected]).Source = _unselectedImageSource;
((Image)_indicators.Children[position]).Source = _selectedImageSource;
}
else
{
var enumerator = ItemsSource.GetEnumerator();
int count = 0;
var count = 0;
while (enumerator.MoveNext())
{
Image image = null;
if (position == count)
image = BuildImage(State.Selected, count);
else
image = BuildImage(State.Unselected, count);

var image = BuildImage(position == count ? State.Selected : State.Unselected, count);
_indicators.Children.Add(image);

count++;
}
}

_currentlySelected = position;
}

private Image BuildImage(State state, int position)
Image BuildImage(State state, int position)
{
var image = new Image()
{
Expand All @@ -119,36 +115,34 @@ private Image BuildImage(State state, int position)
switch (state)
{
case State.Selected:
image.Source = SelectedImageSource;
image.Source = _selectedImageSource;
break;
case State.Unselected:
image.Source = UnselectedImageSource;
image.Source = _unselectedImageSource;
break;
default:
throw new Exception("Invalid state selected");
}

image.GestureRecognizers.Add(new TapGestureRecognizer() { Command = new Command(() => { Position = position; }) });

return image;
}

private static void PositionChanging(object bindable, object oldValue, object newValue)
static void PositionChanging(object bindable, object oldValue, object newValue)
{
var carouselIndicators = bindable as CarouselIndicators;

carouselIndicators.Init(Convert.ToInt32(newValue));
carouselIndicators?.Init(Convert.ToInt32(newValue));
}

private static void ItemsChanged(object bindable, object oldValue, object newValue)
static void ItemsChanged(object bindable, object oldValue, object newValue)
{
var carouselIndicators = bindable as CarouselIndicators;

carouselIndicators.Clear();
carouselIndicators.Init(0);
carouselIndicators?.Clear();
carouselIndicators?.Init(0);
}

public enum State
enum State
{
Selected,
Unselected
Expand Down
6 changes: 4 additions & 2 deletions CarouselViewSample/CarouselViewSample/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@

<ContentView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<StackLayout>
<control:CarouselView ItemsSource="{Binding MyDataSource}" Position="{Binding Position, Mode=TwoWay}">
<control:CarouselView ItemsSource="{Binding MyDataSource}" x:Name="Carousel">
<control:CarouselView.ItemTemplate>
<DataTemplate>
<Label Text="{Binding Name}" TextColor="White" />
</DataTemplate>
</control:CarouselView.ItemTemplate>
</control:CarouselView>
<local:CarouselIndicators IndicatorHeight="16" IndicatorWidth="16" UnselectedIndicator="unselected_circle.png" SelectedIndicator="selected_circle.png" Position="{Binding Position}" ItemsSource="{Binding MyDataSource}" />
<local:CarouselIndicators IndicatorHeight="16" IndicatorWidth="16" UnselectedIndicator="unselected_circle.png"
SelectedIndicator="selected_circle.png" Position="{Binding Position, Source={x:Reference Carousel}}"
ItemsSource="{Binding MyDataSource}" />
</StackLayout>

</ContentView>
Expand Down
20 changes: 9 additions & 11 deletions CarouselViewSample/CarouselViewSample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@ public partial class MainPage : ContentPage, INotifyPropertyChanged
public MainPage()
{
InitializeComponent();

// An Example DataSource
MyDataSource = new List<CarouselData>() { new CarouselData() { Name = "Title1" },
new CarouselData() { Name = "Title2" },
new CarouselData() { Name = "Title3" },
new CarouselData() { Name = "Title4" }};
MyDataSource = new List<CarouselData>()
{
new CarouselData() {Name = "Title1"},
new CarouselData() {Name = "Title2"},
new CarouselData() {Name = "Title3"},
new CarouselData() {Name = "Title4"}
};

BindingContext = this;
}


public List<CarouselData> MyDataSource { get; set; } // Must have default value or be set before the BindingContext is set.

private int _position;
public int Position { get { return _position; } set { _position = value; OnPropertyChanged(); } }


}
}
}