Guest User

Untitled

a guest
Dec 12th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. xmlns:cv="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView"
  2.  
  3. <StackLayout Padding="0,0,0,5" BackgroundColor="#d8d8d8" >
  4. <cv:CarouselView x:Name="cview" ItemsSource="{Binding DataSource}" Position="{Binding Position, Mode=TwoWay}">
  5. <cv:CarouselView.ItemTemplate>
  6. <DataTemplate>
  7. <Image Aspect="AspectFill" HorizontalOptions="Center" VerticalOptions="Center" Source="{Binding PickedImage}" />
  8. </DataTemplate>
  9. </cv:CarouselView.ItemTemplate>
  10. </cv:CarouselView>
  11. <cutomControl:CarouselIndicators IndicatorHeight="16" IndicatorWidth="16" UnselectedIndicator="unselected_circle.png" SelectedIndicator="selected_circle.png" Position="{Binding Position}" ItemsSource="{Binding DataSource}" />
  12. </StackLayout>
  13.  
  14. private int _position;
  15. public int Position
  16. {
  17. get { return _position; }
  18. set
  19. {
  20. _position = value;
  21. OnPropertyChanged();
  22. }
  23. }
  24.  
  25. xmlns:cutomControl="clr-namespace:XXXX.CustomControls;assembly=XXXX"
  26. xmlns:cv="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView"
  27.  
  28. public class CarouselIndicators : Grid
  29. {
  30. private ImageSource UnselectedImageSource = null;
  31. private ImageSource SelectedImageSource = null;
  32. private readonly StackLayout _indicators = new StackLayout() { Orientation = StackOrientation.Horizontal, HorizontalOptions = LayoutOptions.CenterAndExpand };
  33.  
  34. public CarouselIndicators()
  35. {
  36. this.HorizontalOptions = LayoutOptions.CenterAndExpand;
  37. this.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
  38. this.Children.Add(_indicators);
  39. }
  40.  
  41. public static readonly BindableProperty PositionProperty = BindableProperty.Create(nameof(Position), typeof(int), typeof(CarouselIndicators), 0, BindingMode.TwoWay, propertyChanging: PositionChanging);
  42. public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(nameof(ItemsSource), typeof(IEnumerable), typeof(CarouselIndicators), Enumerable.Empty<object>(), BindingMode.OneWay, propertyChanged: ItemsChanged);
  43. public static readonly BindableProperty SelectedIndicatorProperty = BindableProperty.Create(nameof(SelectedIndicator), typeof(string), typeof(CarouselIndicators), "", BindingMode.OneWay);
  44. public static readonly BindableProperty UnselectedIndicatorProperty = BindableProperty.Create(nameof(UnselectedIndicator), typeof(string), typeof(CarouselIndicators), "", BindingMode.OneWay);
  45. public static readonly BindableProperty IndicatorWidthProperty = BindableProperty.Create(nameof(IndicatorWidth), typeof(double), typeof(CarouselIndicators), 0.0, BindingMode.OneWay);
  46. public static readonly BindableProperty IndicatorHeightProperty = BindableProperty.Create(nameof(IndicatorHeight), typeof(double), typeof(CarouselIndicators), 0.0, BindingMode.OneWay);
  47.  
  48. public string SelectedIndicator
  49. {
  50. get { return (string)this.GetValue(SelectedIndicatorProperty); }
  51. set { this.SetValue(SelectedIndicatorProperty, value); }
  52. }
  53.  
  54. public string UnselectedIndicator
  55. {
  56. get { return (string)this.GetValue(UnselectedIndicatorProperty); }
  57. set { this.SetValue(UnselectedIndicatorProperty, value); }
  58. }
  59.  
  60. public double IndicatorWidth
  61. {
  62. get { return (double)this.GetValue(IndicatorWidthProperty); }
  63. set { this.SetValue(IndicatorWidthProperty, value); }
  64. }
  65.  
  66. public double IndicatorHeight
  67. {
  68. get { return (double)this.GetValue(IndicatorHeightProperty); }
  69. set { this.SetValue(IndicatorHeightProperty, value); }
  70. }
  71.  
  72. public int Position
  73. {
  74. get { return (int)this.GetValue(PositionProperty); }
  75. set { this.SetValue(PositionProperty, value); }
  76. }
  77.  
  78. public IEnumerable ItemsSource
  79. {
  80. get { return (IEnumerable)this.GetValue(ItemsSourceProperty); }
  81. set { this.SetValue(ItemsSourceProperty, (object)value); }
  82. }
  83.  
  84. private void Clear()
  85. {
  86. _indicators.Children.Clear();
  87. }
  88.  
  89. private void Init(int position)
  90. {
  91.  
  92. if (UnselectedImageSource == null)
  93. UnselectedImageSource = ImageSource.FromFile(UnselectedIndicator);
  94.  
  95. if (SelectedImageSource == null)
  96. SelectedImageSource = ImageSource.FromFile(SelectedIndicator);
  97.  
  98. if (_indicators.Children.Count > 0)
  99. {
  100. for (int i = 0; i < _indicators.Children.Count; i++)
  101. {
  102. if (((Image)_indicators.Children[i]).ClassId == nameof(State.Selected) && i != position)
  103. _indicators.Children[i] = BuildImage(State.Unselected, i);
  104. else if (((Image)_indicators.Children[i]).ClassId == nameof(State.Unselected) && i == position)
  105. _indicators.Children[i] = BuildImage(State.Selected, i);
  106. }
  107. }
  108. else
  109. {
  110. var enumerator = ItemsSource.GetEnumerator();
  111. int count = 0;
  112. while (enumerator.MoveNext())
  113. {
  114. Image image = null;
  115. if (position == count)
  116. image = BuildImage(State.Selected, count);
  117. else
  118. image = BuildImage(State.Unselected, count);
  119.  
  120. _indicators.Children.Add(image);
  121.  
  122. count++;
  123. }
  124. }
  125. }
  126.  
  127. private Image BuildImage(State state, int position)
  128. {
  129. var image = new Image()
  130. {
  131. WidthRequest = IndicatorWidth,
  132. HeightRequest = IndicatorHeight,
  133. ClassId = state.ToString()
  134. };
  135.  
  136. switch (state)
  137. {
  138. case State.Selected:
  139. image.Source = SelectedImageSource;
  140. break;
  141. case State.Unselected:
  142. image.Source = UnselectedImageSource;
  143. break;
  144. default:
  145. throw new Exception("Invalid state selected");
  146. }
  147.  
  148. image.GestureRecognizers.Add(new TapGestureRecognizer() { Command = new Command(() => { Position = position; }) });
  149.  
  150. return image;
  151. }
  152.  
  153. private static void PositionChanging(object bindable, object oldValue, object newValue)
  154. {
  155. var carouselIndicators = bindable as CarouselIndicators;
  156.  
  157. carouselIndicators.Init(Convert.ToInt32(newValue));
  158. }
  159.  
  160. private static void ItemsChanged(object bindable, object oldValue, object newValue)
  161. {
  162. var carouselIndicators = bindable as CarouselIndicators;
  163.  
  164. carouselIndicators.Clear();
  165. carouselIndicators.Init(0);
  166. }
  167.  
  168. public enum State
  169. {
  170. Selected,
  171. Unselected
  172. }
  173. }
Add Comment
Please, Sign In to add comment