This repository has been archived by the owner on May 1, 2024. It is now read-only.
/
CarouselView.cs
250 lines (192 loc) · 8.12 KB
/
CarouselView.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Xamarin.Forms.Platform;
namespace Xamarin.Forms
{
[RenderWith(typeof(_CarouselViewRenderer))]
public class CarouselView : ItemsView
{
public const string CurrentItemVisualState = "CurrentItem";
public const string NextItemVisualState = "NextItem";
public const string PreviousItemVisualState = "PreviousItem";
public const string DefaultItemVisualState = "DefaultItem";
public static readonly BindableProperty LoopProperty = BindableProperty.Create(nameof(Loop), typeof(bool), typeof(CarouselView), true, BindingMode.OneTime);
public bool Loop
{
get { return (bool)GetValue(LoopProperty); }
set { SetValue(LoopProperty, value); }
}
public static readonly BindableProperty PeekAreaInsetsProperty = BindableProperty.Create(nameof(PeekAreaInsets), typeof(Thickness), typeof(CarouselView), default(Thickness));
public Thickness PeekAreaInsets
{
get { return (Thickness)GetValue(PeekAreaInsetsProperty); }
set { SetValue(PeekAreaInsetsProperty, value); }
}
static readonly BindablePropertyKey VisibleViewsPropertyKey = BindableProperty.CreateReadOnly(nameof(VisibleViews), typeof(ObservableCollection<View>), typeof(CarouselView), null, defaultValueCreator: (b) => new ObservableCollection<View>());
public static readonly BindableProperty VisibleViewsProperty = VisibleViewsPropertyKey.BindableProperty;
public ObservableCollection<View> VisibleViews => (ObservableCollection<View>)GetValue(VisibleViewsProperty);
static readonly BindablePropertyKey IsDraggingPropertyKey = BindableProperty.CreateReadOnly(nameof(IsDragging), typeof(bool), typeof(CarouselView), false);
public static readonly BindableProperty IsDraggingProperty = IsDraggingPropertyKey.BindableProperty;
public bool IsDragging => (bool)GetValue(IsDraggingProperty);
public static readonly BindableProperty IsBounceEnabledProperty =
BindableProperty.Create(nameof(IsBounceEnabled), typeof(bool), typeof(CarouselView), true);
public bool IsBounceEnabled
{
get { return (bool)GetValue(IsBounceEnabledProperty); }
set { SetValue(IsBounceEnabledProperty, value); }
}
public static readonly BindableProperty IsSwipeEnabledProperty =
BindableProperty.Create(nameof(IsSwipeEnabled), typeof(bool), typeof(CarouselView), true);
public bool IsSwipeEnabled
{
get { return (bool)GetValue(IsSwipeEnabledProperty); }
set { SetValue(IsSwipeEnabledProperty, value); }
}
public static readonly BindableProperty IsScrollAnimatedProperty =
BindableProperty.Create(nameof(IsScrollAnimated), typeof(bool), typeof(CarouselView), true);
public bool IsScrollAnimated
{
get { return (bool)GetValue(IsScrollAnimatedProperty); }
set { SetValue(IsScrollAnimatedProperty, value); }
}
public static readonly BindableProperty CurrentItemProperty =
BindableProperty.Create(nameof(CurrentItem), typeof(object), typeof(CarouselView), default, BindingMode.TwoWay,
propertyChanged: CurrentItemPropertyChanged);
public static readonly BindableProperty CurrentItemChangedCommandProperty =
BindableProperty.Create(nameof(CurrentItemChangedCommand), typeof(ICommand), typeof(CarouselView));
public static readonly BindableProperty CurrentItemChangedCommandParameterProperty =
BindableProperty.Create(nameof(CurrentItemChangedCommandParameter), typeof(object), typeof(CarouselView));
public object CurrentItem
{
get => GetValue(CurrentItemProperty);
set => SetValue(CurrentItemProperty, value);
}
public ICommand CurrentItemChangedCommand
{
get => (ICommand)GetValue(CurrentItemChangedCommandProperty);
set => SetValue(CurrentItemChangedCommandProperty, value);
}
public object CurrentItemChangedCommandParameter
{
get => GetValue(CurrentItemChangedCommandParameterProperty);
set => SetValue(CurrentItemChangedCommandParameterProperty, value);
}
static void CurrentItemPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var carouselView = (CarouselView)bindable;
var args = new CurrentItemChangedEventArgs(oldValue, newValue);
var command = carouselView.CurrentItemChangedCommand;
if (command != null)
{
var commandParameter = carouselView.CurrentItemChangedCommandParameter;
if (command.CanExecute(commandParameter))
{
command.Execute(commandParameter);
}
}
carouselView.CurrentItemChanged?.Invoke(carouselView, args);
carouselView.OnCurrentItemChanged(args);
}
public static readonly BindableProperty PositionProperty =
BindableProperty.Create(nameof(Position), typeof(int), typeof(CarouselView), default(int), BindingMode.TwoWay,
propertyChanged: PositionPropertyChanged);
public static readonly BindableProperty PositionChangedCommandProperty =
BindableProperty.Create(nameof(PositionChangedCommand), typeof(ICommand), typeof(CarouselView));
public static readonly BindableProperty PositionChangedCommandParameterProperty =
BindableProperty.Create(nameof(PositionChangedCommandParameter), typeof(object),
typeof(CarouselView));
public int Position
{
get => (int)GetValue(PositionProperty);
set => SetValue(PositionProperty, value);
}
public ICommand PositionChangedCommand
{
get => (ICommand)GetValue(PositionChangedCommandProperty);
set => SetValue(PositionChangedCommandProperty, value);
}
public object PositionChangedCommandParameter
{
get => GetValue(PositionChangedCommandParameterProperty);
set => SetValue(PositionChangedCommandParameterProperty, value);
}
public static readonly BindableProperty ItemsLayoutProperty =
BindableProperty.Create(nameof(ItemsLayout), typeof(LinearItemsLayout), typeof(ItemsView),
LinearItemsLayout.CarouselDefault);
[TypeConverter(typeof(CarouselLayoutTypeConverter))]
public LinearItemsLayout ItemsLayout
{
get => (LinearItemsLayout)GetValue(ItemsLayoutProperty);
set => SetValue(ItemsLayoutProperty, value);
}
[TypeConverter(typeof(ReferenceTypeConverter))]
public IndicatorView IndicatorView
{
set => LinkToIndicatorView(this, value);
}
static void LinkToIndicatorView(CarouselView carouselView, IndicatorView indicatorView)
{
if (indicatorView == null)
return;
indicatorView.SetBinding(IndicatorView.PositionProperty, new Binding
{
Path = nameof(CarouselView.Position),
Source = carouselView
});
indicatorView.SetBinding(IndicatorView.ItemsSourceProperty, new Binding
{
Path = nameof(ItemsView.ItemsSource),
Source = carouselView
});
}
[EditorBrowsable(EditorBrowsableState.Never)]
public bool IsScrolling { get; set; }
public event EventHandler<CurrentItemChangedEventArgs> CurrentItemChanged;
public event EventHandler<PositionChangedEventArgs> PositionChanged;
public CarouselView()
{
ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Horizontal)
{
SnapPointsType = SnapPointsType.MandatorySingle,
SnapPointsAlignment = SnapPointsAlignment.Center
};
}
protected virtual void OnPositionChanged(PositionChangedEventArgs args)
{
}
protected virtual void OnCurrentItemChanged(EventArgs args)
{
}
static void PositionPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var carousel = (CarouselView)bindable;
var args = new PositionChangedEventArgs((int)oldValue, (int)newValue);
var command = carousel.PositionChangedCommand;
if (command != null)
{
var commandParameter = carousel.PositionChangedCommandParameter;
if (command.CanExecute(commandParameter))
{
command.Execute(commandParameter);
}
}
carousel.PositionChanged?.Invoke(carousel, args);
carousel.OnPositionChanged(args);
}
[EditorBrowsable(EditorBrowsableState.Never)]
public void SetIsDragging(bool value)
{
SetValue(IsDraggingPropertyKey, value);
}
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual bool AnimatePositionChanges => IsScrollAnimated;
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual bool AnimateCurrentItemChanges => IsScrollAnimated;
}
}