From e2068aa529d4792055376cc91bae3a88714c81f7 Mon Sep 17 00:00:00 2001 From: Ashley Jackson Date: Mon, 9 Jan 2017 11:41:28 +0000 Subject: [PATCH 1/3] Updated Indicator image switch logic Removed need for Position Property --- .../CarouselViewSample.Droid.csproj | 2 +- .../CarouselViewSample.Droid.csproj.bak | 161 ++++++++++++++++++ .../CarouselViewSample/CarouselIndicators.cs | 122 ++++++------- .../CarouselViewSample/MainPage.xaml.cs | 20 +-- 4 files changed, 233 insertions(+), 72 deletions(-) create mode 100644 CarouselViewSample/CarouselViewSample.Droid/CarouselViewSample.Droid.csproj.bak 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.Droid/CarouselViewSample.Droid.csproj.bak b/CarouselViewSample/CarouselViewSample.Droid/CarouselViewSample.Droid.csproj.bak new file mode 100644 index 0000000..ffd06c3 --- /dev/null +++ b/CarouselViewSample/CarouselViewSample.Droid/CarouselViewSample.Droid.csproj.bak @@ -0,0 +1,161 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {8CD5361D-9669-4F29-9E20-B8823B91C1BE} + {EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + Library + Properties + CarouselViewSample.Droid + CarouselViewSample.Droid + 512 + true + Resources\Resource.Designer.cs + Off + Properties\AndroidManifest.xml + true + v7.0 + armeabi,armeabi-v7a,x86 + + + + + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + True + None + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + False + SdkOnly + + + + ..\..\packages\Xamarin.Forms.2.3.3.168\lib\MonoAndroid10\FormsViewGroup.dll + True + + + + + + + + + + ..\..\packages\Xamarin.Android.Support.Animated.Vector.Drawable.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.Animated.Vector.Drawable.dll + True + + + ..\..\packages\Xamarin.Android.Support.Design.23.3.0\lib\MonoAndroid43\Xamarin.Android.Support.Design.dll + True + + + ..\..\packages\Xamarin.Android.Support.v4.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.v4.dll + True + + + ..\..\packages\Xamarin.Android.Support.v7.AppCompat.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.v7.AppCompat.dll + True + + + ..\..\packages\Xamarin.Android.Support.v7.CardView.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.v7.CardView.dll + True + + + ..\..\packages\Xamarin.Android.Support.v7.MediaRouter.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.v7.MediaRouter.dll + True + + + ..\..\packages\Xamarin.Android.Support.v7.RecyclerView.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.v7.RecyclerView.dll + True + + + ..\..\packages\Xamarin.Android.Support.Vector.Drawable.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.Vector.Drawable.dll + True + + + ..\..\packages\Xamarin.Forms.CarouselView.2.3.0-pre2\lib\MonoAndroid10\Xamarin.Forms.CarouselView.dll + True + + + ..\..\packages\Xamarin.Forms.2.3.3.168\lib\MonoAndroid10\Xamarin.Forms.Core.dll + True + + + ..\..\packages\Xamarin.Forms.2.3.3.168\lib\MonoAndroid10\Xamarin.Forms.Platform.dll + True + + + ..\..\packages\Xamarin.Forms.2.3.3.168\lib\MonoAndroid10\Xamarin.Forms.Platform.Android.dll + True + + + ..\..\packages\Xamarin.Forms.2.3.3.168\lib\MonoAndroid10\Xamarin.Forms.Xaml.dll + True + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {a5b90018-55b1-4a7d-9016-b664f36f3424} + CarouselViewSample + + + + + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + \ No newline at end of file diff --git a/CarouselViewSample/CarouselViewSample/CarouselIndicators.cs b/CarouselViewSample/CarouselViewSample/CarouselIndicators.cs index 1b624f5..6aed9bf 100644 --- a/CarouselViewSample/CarouselViewSample/CarouselIndicators.cs +++ b/CarouselViewSample/CarouselViewSample/CarouselIndicators.cs @@ -1,113 +1,117 @@ 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() + public CarouselIndicators(int currentlySelected) { - this.HorizontalOptions = LayoutOptions.CenterAndExpand; - this.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); - this.Children.Add(_indicators); + _currentlySelected = currentlySelected; + HorizontalOptions = LayoutOptions.CenterAndExpand; + RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); + 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), "", 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 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 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 +123,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.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 From 3c0b6e03566e58f6caeea11e78548d8d0c313183 Mon Sep 17 00:00:00 2001 From: Ashley Jackson Date: Mon, 9 Jan 2017 11:47:44 +0000 Subject: [PATCH 2/3] Added x:reference for Position Property --- .../CarouselViewSample/CarouselIndicators.cs | 8 -------- CarouselViewSample/CarouselViewSample/MainPage.xaml | 6 ++++-- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/CarouselViewSample/CarouselViewSample/CarouselIndicators.cs b/CarouselViewSample/CarouselViewSample/CarouselIndicators.cs index 6aed9bf..ace01e1 100644 --- a/CarouselViewSample/CarouselViewSample/CarouselIndicators.cs +++ b/CarouselViewSample/CarouselViewSample/CarouselIndicators.cs @@ -11,14 +11,6 @@ public class CarouselIndicators : Grid ImageSource _selectedImageSource; readonly StackLayout _indicators = new StackLayout { Orientation = StackOrientation.Horizontal, HorizontalOptions = LayoutOptions.CenterAndExpand }; - public CarouselIndicators(int currentlySelected) - { - _currentlySelected = currentlySelected; - HorizontalOptions = LayoutOptions.CenterAndExpand; - RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); - Children.Add(_indicators); - } - public static readonly BindableProperty PositionProperty = BindableProperty.Create(nameof(Position), typeof(int), typeof(CarouselIndicators), 0, BindingMode.TwoWay, propertyChanging: PositionChanging); 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 @@ - + - + From ccb2e8ffa357dc76f74de452ee7007e5bec59b93 Mon Sep 17 00:00:00 2001 From: Ashley Jackson Date: Mon, 9 Jan 2017 11:49:36 +0000 Subject: [PATCH 3/3] Removed bak file --- .../CarouselViewSample.Droid.csproj.bak | 161 ------------------ 1 file changed, 161 deletions(-) delete mode 100644 CarouselViewSample/CarouselViewSample.Droid/CarouselViewSample.Droid.csproj.bak diff --git a/CarouselViewSample/CarouselViewSample.Droid/CarouselViewSample.Droid.csproj.bak b/CarouselViewSample/CarouselViewSample.Droid/CarouselViewSample.Droid.csproj.bak deleted file mode 100644 index ffd06c3..0000000 --- a/CarouselViewSample/CarouselViewSample.Droid/CarouselViewSample.Droid.csproj.bak +++ /dev/null @@ -1,161 +0,0 @@ - - - - Debug - AnyCPU - 8.0.30703 - 2.0 - {8CD5361D-9669-4F29-9E20-B8823B91C1BE} - {EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - Library - Properties - CarouselViewSample.Droid - CarouselViewSample.Droid - 512 - true - Resources\Resource.Designer.cs - Off - Properties\AndroidManifest.xml - true - v7.0 - armeabi,armeabi-v7a,x86 - - - - - - - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - True - None - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - False - SdkOnly - - - - ..\..\packages\Xamarin.Forms.2.3.3.168\lib\MonoAndroid10\FormsViewGroup.dll - True - - - - - - - - - - ..\..\packages\Xamarin.Android.Support.Animated.Vector.Drawable.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.Animated.Vector.Drawable.dll - True - - - ..\..\packages\Xamarin.Android.Support.Design.23.3.0\lib\MonoAndroid43\Xamarin.Android.Support.Design.dll - True - - - ..\..\packages\Xamarin.Android.Support.v4.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.v4.dll - True - - - ..\..\packages\Xamarin.Android.Support.v7.AppCompat.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.v7.AppCompat.dll - True - - - ..\..\packages\Xamarin.Android.Support.v7.CardView.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.v7.CardView.dll - True - - - ..\..\packages\Xamarin.Android.Support.v7.MediaRouter.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.v7.MediaRouter.dll - True - - - ..\..\packages\Xamarin.Android.Support.v7.RecyclerView.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.v7.RecyclerView.dll - True - - - ..\..\packages\Xamarin.Android.Support.Vector.Drawable.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.Vector.Drawable.dll - True - - - ..\..\packages\Xamarin.Forms.CarouselView.2.3.0-pre2\lib\MonoAndroid10\Xamarin.Forms.CarouselView.dll - True - - - ..\..\packages\Xamarin.Forms.2.3.3.168\lib\MonoAndroid10\Xamarin.Forms.Core.dll - True - - - ..\..\packages\Xamarin.Forms.2.3.3.168\lib\MonoAndroid10\Xamarin.Forms.Platform.dll - True - - - ..\..\packages\Xamarin.Forms.2.3.3.168\lib\MonoAndroid10\Xamarin.Forms.Platform.Android.dll - True - - - ..\..\packages\Xamarin.Forms.2.3.3.168\lib\MonoAndroid10\Xamarin.Forms.Xaml.dll - True - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {a5b90018-55b1-4a7d-9016-b664f36f3424} - CarouselViewSample - - - - - - - - - - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - - - - \ No newline at end of file