22using System . Collections ;
33using System . Collections . Generic ;
44using System . Linq ;
5- using System . Timers ;
65using Xamarin . Forms ;
6+ using System . Threading . Tasks ;
77
88namespace CustomLayouts . Controls
99{
@@ -17,7 +17,6 @@ public enum IndicatorStyleEnum
1717 }
1818
1919 readonly StackLayout _stack ;
20- Timer _selectedItemTimer ;
2120
2221 int _selectedIndex ;
2322
@@ -31,13 +30,6 @@ public CarouselLayout ()
3130 } ;
3231
3332 Content = _stack ;
34-
35- _selectedItemTimer = new Timer {
36- AutoReset = false ,
37- Interval = 300
38- } ;
39-
40- _selectedItemTimer . Elapsed += SelectedItemTimerElapsed ;
4133 }
4234
4335 public IndicatorStyleEnum IndicatorStyle { get ; set ; }
@@ -60,13 +52,16 @@ protected override void LayoutChildren (double x, double y, double width, double
6052 }
6153
6254 public static readonly BindableProperty SelectedIndexProperty =
63- BindableProperty . Create < CarouselLayout , int > (
64- carousel => carousel . SelectedIndex ,
65- 0 ,
55+ BindableProperty . Create (
56+ nameof ( SelectedIndex ) ,
57+ typeof ( int ) ,
58+ typeof ( CarouselLayout ) ,
59+ 0 ,
6660 BindingMode . TwoWay ,
67- propertyChanged : ( bindable , oldValue , newValue ) => {
68- ( ( CarouselLayout ) bindable ) . UpdateSelectedItem ( ) ;
69- }
61+ propertyChanged : async ( bindable , oldValue , newValue ) =>
62+ {
63+ await ( ( CarouselLayout ) bindable ) . UpdateSelectedItem ( ) ;
64+ }
7065 ) ;
7166
7267 public int SelectedIndex {
@@ -78,26 +73,26 @@ public int SelectedIndex {
7873 }
7974 }
8075
81- void UpdateSelectedItem ( )
76+ async Task UpdateSelectedItem ( )
8277 {
83- _selectedItemTimer . Stop ( ) ;
84- _selectedItemTimer . Start ( ) ;
85- }
86-
87- void SelectedItemTimerElapsed ( object sender , ElapsedEventArgs e ) {
88- SelectedItem = SelectedIndex > - 1 ? Children [ SelectedIndex ] . BindingContext : null ;
78+ await Task . Delay ( 300 ) ;
79+ SelectedItem = SelectedIndex > - 1 ? Children [ SelectedIndex ] . BindingContext : null ;
8980 }
9081
9182 public static readonly BindableProperty ItemsSourceProperty =
92- BindableProperty . Create < CarouselLayout , IList > (
93- view => view . ItemsSource ,
83+ BindableProperty . Create (
84+ nameof ( ItemsSource ) ,
85+ typeof ( IList ) ,
86+ typeof ( CarouselLayout ) ,
9487 null ,
95- propertyChanging : ( bindableObject , oldValue , newValue ) => {
96- ( ( CarouselLayout ) bindableObject ) . ItemsSourceChanging ( ) ;
97- } ,
98- propertyChanged : ( bindableObject , oldValue , newValue ) => {
99- ( ( CarouselLayout ) bindableObject ) . ItemsSourceChanged ( ) ;
100- }
88+ propertyChanging : ( bindableObject , oldValue , newValue ) =>
89+ {
90+ ( ( CarouselLayout ) bindableObject ) . ItemsSourceChanging ( ) ;
91+ } ,
92+ propertyChanged : ( bindableObject , oldValue , newValue ) =>
93+ {
94+ ( ( CarouselLayout ) bindableObject ) . ItemsSourceChanged ( ) ;
95+ }
10196 ) ;
10297
10398 public IList ItemsSource {
@@ -134,14 +129,17 @@ public DataTemplate ItemTemplate {
134129 set ;
135130 }
136131
137- public static readonly BindableProperty SelectedItemProperty =
138- BindableProperty . Create < CarouselLayout , object > (
139- view => view . SelectedItem ,
132+ public static readonly BindableProperty SelectedItemProperty =
133+ BindableProperty . Create (
134+ nameof ( SelectedItem ) ,
135+ typeof ( object ) ,
136+ typeof ( CarouselLayout ) ,
140137 null ,
141138 BindingMode . TwoWay ,
142- propertyChanged : ( bindable , oldValue , newValue ) => {
143- ( ( CarouselLayout ) bindable ) . UpdateSelectedIndex ( ) ;
144- }
139+ propertyChanged : ( bindable , oldValue , newValue ) =>
140+ {
141+ ( ( CarouselLayout ) bindable ) . UpdateSelectedIndex ( ) ;
142+ }
145143 ) ;
146144
147145 public object SelectedItem {
0 commit comments