Skip to content

Commit e4f2330

Browse files
committed
Fixed some deprecated Forms code
1 parent 980975a commit e4f2330

File tree

3 files changed

+76
-64
lines changed

3 files changed

+76
-64
lines changed

src/CustomLayouts/Controls/CarouselLayout.cs

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Linq;
5-
using System.Timers;
65
using Xamarin.Forms;
6+
using System.Threading.Tasks;
77

88
namespace 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 {

src/CustomLayouts/PagerIndicatorDots.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public interface ITabProvider
1212

1313
public class PagerIndicatorDots : StackLayout
1414
{
15-
int dotCount = 1;
1615
int _selectedIndex;
1716

1817
public Color DotColor { get; set; }
@@ -54,16 +53,20 @@ void CreateTabs()
5453
}
5554

5655
public static BindableProperty ItemsSourceProperty =
57-
BindableProperty.Create<PagerIndicatorDots, IList> (
58-
pi => pi.ItemsSource,
56+
BindableProperty.Create(
57+
nameof(ItemsSource),
58+
typeof(IList),
59+
typeof(PagerIndicatorDots),
5960
null,
6061
BindingMode.OneWay,
61-
propertyChanging: (bindable, oldValue, newValue) => {
62-
((PagerIndicatorDots)bindable).ItemsSourceChanging ();
63-
},
64-
propertyChanged: (bindable, oldValue, newValue) => {
65-
((PagerIndicatorDots)bindable).ItemsSourceChanged ();
66-
}
62+
propertyChanging: (bindable, oldValue, newValue) =>
63+
{
64+
((PagerIndicatorDots)bindable).ItemsSourceChanging();
65+
},
66+
propertyChanged: (bindable, oldValue, newValue) =>
67+
{
68+
((PagerIndicatorDots)bindable).ItemsSourceChanged();
69+
}
6770
);
6871

6972
public IList ItemsSource {
@@ -76,13 +79,17 @@ public IList ItemsSource {
7679
}
7780

7881
public static BindableProperty SelectedItemProperty =
79-
BindableProperty.Create<PagerIndicatorDots, object> (
80-
pi => pi.SelectedItem,
82+
BindableProperty.Create(
83+
nameof(SelectedItem),
84+
typeof(object),
85+
typeof(PagerIndicatorDots),
8186
null,
8287
BindingMode.TwoWay,
83-
propertyChanged: (bindable, oldValue, newValue) => {
84-
((PagerIndicatorDots)bindable).SelectedItemChanged ();
85-
});
88+
propertyChanged: (bindable, oldValue, newValue) =>
89+
{
90+
((PagerIndicatorDots)bindable).SelectedItemChanged();
91+
}
92+
);
8693

8794
public object SelectedItem {
8895
get {

src/CustomLayouts/PagerIndicatorTabs.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ namespace CustomLayouts
88
{
99
public class PagerIndicatorTabs : Grid
1010
{
11-
int dotCount = 1;
1211
int _selectedIndex;
1312

1413
public Color DotColor { get; set; }
@@ -63,16 +62,20 @@ void CreateTabs()
6362
}
6463

6564
public static BindableProperty ItemsSourceProperty =
66-
BindableProperty.Create<PagerIndicatorTabs, IList> (
67-
pi => pi.ItemsSource,
65+
BindableProperty.Create(
66+
nameof(ItemsSource),
67+
typeof(IList),
68+
typeof(PagerIndicatorTabs),
6869
null,
6970
BindingMode.OneWay,
70-
propertyChanging: (bindable, oldValue, newValue) => {
71-
((PagerIndicatorTabs)bindable).ItemsSourceChanging ();
72-
},
73-
propertyChanged: (bindable, oldValue, newValue) => {
74-
((PagerIndicatorTabs)bindable).ItemsSourceChanged ();
75-
}
71+
propertyChanging: (bindable, oldValue, newValue) =>
72+
{
73+
((PagerIndicatorTabs)bindable).ItemsSourceChanging();
74+
},
75+
propertyChanged: (bindable, oldValue, newValue) =>
76+
{
77+
((PagerIndicatorTabs)bindable).ItemsSourceChanged();
78+
}
7679
);
7780

7881
public IList ItemsSource {
@@ -85,13 +88,17 @@ public IList ItemsSource {
8588
}
8689

8790
public static BindableProperty SelectedItemProperty =
88-
BindableProperty.Create<PagerIndicatorTabs, object> (
89-
pi => pi.SelectedItem,
91+
BindableProperty.Create(
92+
nameof(SelectedItem),
93+
typeof(object),
94+
typeof(PagerIndicatorTabs),
9095
null,
9196
BindingMode.TwoWay,
92-
propertyChanged: (bindable, oldValue, newValue) => {
93-
((PagerIndicatorTabs)bindable).SelectedItemChanged ();
94-
});
97+
propertyChanged: (bindable, oldValue, newValue) =>
98+
{
99+
((PagerIndicatorTabs)bindable).SelectedItemChanged();
100+
}
101+
);
95102

96103
public object SelectedItem {
97104
get {

0 commit comments

Comments
 (0)