diff --git a/CarouselViewSample/CarouselViewSample.Droid/CarouselViewSample.Droid.csproj b/CarouselViewSample/CarouselViewSample.Droid/CarouselViewSample.Droid.csproj index ffd06c3..5766151 100644 --- a/CarouselViewSample/CarouselViewSample.Droid/CarouselViewSample.Droid.csproj +++ b/CarouselViewSample/CarouselViewSample.Droid/CarouselViewSample.Droid.csproj @@ -17,7 +17,7 @@ Off Properties\AndroidManifest.xml true - v7.0 + v6.0 armeabi,armeabi-v7a,x86 diff --git a/CarouselViewSample/CarouselViewSample/CarouselIndicators.cs b/CarouselViewSample/CarouselViewSample/CarouselIndicators.cs index 1b624f5..ace01e1 100644 --- a/CarouselViewSample/CarouselViewSample/CarouselIndicators.cs +++ b/CarouselViewSample/CarouselViewSample/CarouselIndicators.cs @@ -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(), 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(), 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() { @@ -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 diff --git a/CarouselViewSample/CarouselViewSample/MainPage.xaml b/CarouselViewSample/CarouselViewSample/MainPage.xaml index 26fd888..b04cc07 100644 --- a/CarouselViewSample/CarouselViewSample/MainPage.xaml +++ b/CarouselViewSample/CarouselViewSample/MainPage.xaml @@ -8,14 +8,16 @@ - + - + diff --git a/CarouselViewSample/CarouselViewSample/MainPage.xaml.cs b/CarouselViewSample/CarouselViewSample/MainPage.xaml.cs index 8fcda0a..549314f 100644 --- a/CarouselViewSample/CarouselViewSample/MainPage.xaml.cs +++ b/CarouselViewSample/CarouselViewSample/MainPage.xaml.cs @@ -13,22 +13,20 @@ public partial class MainPage : ContentPage, INotifyPropertyChanged public MainPage() { InitializeComponent(); - + // An Example DataSource - MyDataSource = new List() { new CarouselData() { Name = "Title1" }, - new CarouselData() { Name = "Title2" }, - new CarouselData() { Name = "Title3" }, - new CarouselData() { Name = "Title4" }}; + MyDataSource = new List() + { + new CarouselData() {Name = "Title1"}, + new CarouselData() {Name = "Title2"}, + new CarouselData() {Name = "Title3"}, + new CarouselData() {Name = "Title4"} + }; BindingContext = this; } public List 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(); } } - - } -} +} \ No newline at end of file