|
6 | 6 |
|
7 | 7 | namespace CustomLayouts |
8 | 8 | { |
9 | | - public class PagerIndicatorTabs : Grid |
10 | | - { |
11 | | - int _selectedIndex; |
12 | | - |
13 | | - public Color DotColor { get; set; } |
14 | | - public double DotSize { get; set; } |
15 | | - |
16 | | - public PagerIndicatorTabs() |
17 | | - { |
18 | | - HorizontalOptions = LayoutOptions.CenterAndExpand; |
19 | | - VerticalOptions = LayoutOptions.Center; |
20 | | - DotColor = Color.Black; |
21 | | - Device.OnPlatform(iOS: () => BackgroundColor = Color.Gray); |
22 | | - |
23 | | - var assembly = typeof(PagerIndicatorTabs).GetTypeInfo().Assembly; |
24 | | - foreach (var res in assembly.GetManifestResourceNames()) |
25 | | - System.Diagnostics.Debug.WriteLine("found resource: " + res); |
26 | | - } |
27 | | - |
28 | | - void CreateTabs() |
29 | | - { |
30 | | - |
31 | | - if(Children != null && Children.Count > 0) Children.Clear(); |
32 | | - |
33 | | - foreach(var item in ItemsSource) |
34 | | - { |
35 | | - |
36 | | - var index = Children.Count; |
37 | | - var tab = new StackLayout { |
38 | | - Orientation = StackOrientation.Vertical, |
39 | | - HorizontalOptions = LayoutOptions.Center, |
40 | | - VerticalOptions = LayoutOptions.Center, |
41 | | - Padding = new Thickness(7), |
42 | | - }; |
43 | | - Device.OnPlatform( |
44 | | - iOS: () => |
45 | | - { |
46 | | - tab.Children.Add(new Image { Source = "pin.png", HeightRequest = 20 }); |
47 | | - tab.Children.Add(new Label { Text = "Tab " + (index + 1), FontSize = 11 }); |
48 | | - }, |
49 | | - Android: () => |
50 | | - { |
51 | | - tab.Children.Add(new Image { Source = "pin.png", HeightRequest = 25 }); |
52 | | - } |
53 | | - ); |
54 | | - var tgr = new TapGestureRecognizer(); |
55 | | - tgr.Command = new Command(() => |
56 | | - { |
57 | | - SelectedItem = ItemsSource[index]; |
58 | | - }); |
59 | | - tab.GestureRecognizers.Add(tgr); |
60 | | - Children.Add(tab, index, 0); |
61 | | - } |
62 | | - } |
63 | | - |
64 | | - public static BindableProperty ItemsSourceProperty = |
65 | | - BindableProperty.Create( |
66 | | - nameof(ItemsSource), |
67 | | - typeof(IList), |
68 | | - typeof(PagerIndicatorTabs), |
69 | | - null, |
70 | | - BindingMode.OneWay, |
71 | | - propertyChanging: (bindable, oldValue, newValue) => |
72 | | - { |
73 | | - ((PagerIndicatorTabs)bindable).ItemsSourceChanging(); |
74 | | - }, |
75 | | - propertyChanged: (bindable, oldValue, newValue) => |
76 | | - { |
77 | | - ((PagerIndicatorTabs)bindable).ItemsSourceChanged(); |
78 | | - } |
79 | | - ); |
80 | | - |
81 | | - public IList ItemsSource { |
82 | | - get { |
83 | | - return (IList)GetValue(ItemsSourceProperty); |
84 | | - } |
85 | | - set { |
86 | | - SetValue (ItemsSourceProperty, value); |
87 | | - } |
88 | | - } |
89 | | - |
90 | | - public static BindableProperty SelectedItemProperty = |
91 | | - BindableProperty.Create( |
92 | | - nameof(SelectedItem), |
93 | | - typeof(object), |
94 | | - typeof(PagerIndicatorTabs), |
95 | | - null, |
96 | | - BindingMode.TwoWay, |
97 | | - propertyChanged: (bindable, oldValue, newValue) => |
98 | | - { |
99 | | - ((PagerIndicatorTabs)bindable).SelectedItemChanged(); |
100 | | - } |
101 | | - ); |
102 | | - |
103 | | - public object SelectedItem { |
104 | | - get { |
105 | | - return GetValue (SelectedItemProperty); |
106 | | - } |
107 | | - set { |
108 | | - SetValue (SelectedItemProperty, value); |
109 | | - } |
110 | | - } |
111 | | - |
112 | | - void ItemsSourceChanging () |
113 | | - { |
114 | | - if (ItemsSource != null) |
115 | | - _selectedIndex = ItemsSource.IndexOf (SelectedItem); |
116 | | - } |
117 | | - |
118 | | - void ItemsSourceChanged () |
119 | | - { |
120 | | - if (ItemsSource == null) return; |
121 | | - |
122 | | - this.ColumnDefinitions.Clear(); |
123 | | - foreach(var item in ItemsSource) |
124 | | - { |
125 | | - this.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }); |
126 | | - } |
127 | | - |
128 | | - CreateTabs(); |
129 | | - } |
130 | | - |
131 | | - void SelectedItemChanged () { |
132 | | - |
133 | | - var selectedIndex = ItemsSource.IndexOf (SelectedItem); |
134 | | - var pagerIndicators = Children.Cast<StackLayout> ().ToList (); |
135 | | - |
136 | | - foreach(var pi in pagerIndicators) |
137 | | - { |
138 | | - UnselectTab(pi); |
139 | | - } |
140 | | - |
141 | | - if(selectedIndex > -1) |
142 | | - { |
143 | | - SelectTab(pagerIndicators[selectedIndex]); |
144 | | - } |
145 | | - } |
146 | | - |
147 | | - static void UnselectTab (StackLayout tab) |
148 | | - { |
149 | | - tab.Opacity = 0.5; |
150 | | - } |
151 | | - |
152 | | - static void SelectTab (StackLayout tab) |
153 | | - { |
154 | | - tab.Opacity = 1.0; |
155 | | - } |
156 | | - } |
| 9 | + public class PagerIndicatorTabs : Grid |
| 10 | + { |
| 11 | + int _selectedIndex; |
| 12 | + |
| 13 | + public Color DotColor { get; set; } |
| 14 | + public double DotSize { get; set; } |
| 15 | + |
| 16 | + public PagerIndicatorTabs() |
| 17 | + { |
| 18 | + HorizontalOptions = LayoutOptions.CenterAndExpand; |
| 19 | + VerticalOptions = LayoutOptions.Center; |
| 20 | + DotColor = Color.Black; |
| 21 | + switch (Device.RuntimePlatform) |
| 22 | + { |
| 23 | + case Device.iOS: |
| 24 | + BackgroundColor = Color.Gray; |
| 25 | + break; |
| 26 | + } |
| 27 | + |
| 28 | + var assembly = typeof(PagerIndicatorTabs).GetTypeInfo().Assembly; |
| 29 | + foreach (var res in assembly.GetManifestResourceNames()) |
| 30 | + System.Diagnostics.Debug.WriteLine("found resource: " + res); |
| 31 | + } |
| 32 | + |
| 33 | + void CreateTabs() |
| 34 | + { |
| 35 | + |
| 36 | + if (Children != null && Children.Count > 0) Children.Clear(); |
| 37 | + |
| 38 | + foreach (var item in ItemsSource) |
| 39 | + { |
| 40 | + |
| 41 | + var index = Children.Count; |
| 42 | + var tab = new StackLayout |
| 43 | + { |
| 44 | + Orientation = StackOrientation.Vertical, |
| 45 | + HorizontalOptions = LayoutOptions.Center, |
| 46 | + VerticalOptions = LayoutOptions.Center, |
| 47 | + Padding = new Thickness(7), |
| 48 | + }; |
| 49 | + switch (Device.RuntimePlatform) |
| 50 | + { |
| 51 | + case Device.iOS: |
| 52 | + tab.Children.Add(new Image { Source = "pin.png", HeightRequest = 20 }); |
| 53 | + tab.Children.Add(new Label { Text = "Tab " + (index + 1), FontSize = 11 }); |
| 54 | + break; |
| 55 | + |
| 56 | + case Device.Android: |
| 57 | + tab.Children.Add(new Image { Source = "pin.png", HeightRequest = 25 }); |
| 58 | + break; |
| 59 | + } |
| 60 | + |
| 61 | + var tgr = new TapGestureRecognizer(); |
| 62 | + tgr.Command = new Command(() => |
| 63 | + { |
| 64 | + SelectedItem = ItemsSource[index]; |
| 65 | + }); |
| 66 | + tab.GestureRecognizers.Add(tgr); |
| 67 | + Children.Add(tab, index, 0); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static BindableProperty ItemsSourceProperty = |
| 72 | + BindableProperty.Create( |
| 73 | + nameof(ItemsSource), |
| 74 | + typeof(IList), |
| 75 | + typeof(PagerIndicatorTabs), |
| 76 | + null, |
| 77 | + BindingMode.OneWay, |
| 78 | + propertyChanging: (bindable, oldValue, newValue) => |
| 79 | + { |
| 80 | + ((PagerIndicatorTabs)bindable).ItemsSourceChanging(); |
| 81 | + }, |
| 82 | + propertyChanged: (bindable, oldValue, newValue) => |
| 83 | + { |
| 84 | + ((PagerIndicatorTabs)bindable).ItemsSourceChanged(); |
| 85 | + } |
| 86 | + ); |
| 87 | + |
| 88 | + public IList ItemsSource |
| 89 | + { |
| 90 | + get |
| 91 | + { |
| 92 | + return (IList)GetValue(ItemsSourceProperty); |
| 93 | + } |
| 94 | + set |
| 95 | + { |
| 96 | + SetValue(ItemsSourceProperty, value); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public static BindableProperty SelectedItemProperty = |
| 101 | + BindableProperty.Create( |
| 102 | + nameof(SelectedItem), |
| 103 | + typeof(object), |
| 104 | + typeof(PagerIndicatorTabs), |
| 105 | + null, |
| 106 | + BindingMode.TwoWay, |
| 107 | + propertyChanged: (bindable, oldValue, newValue) => |
| 108 | + { |
| 109 | + ((PagerIndicatorTabs)bindable).SelectedItemChanged(); |
| 110 | + } |
| 111 | + ); |
| 112 | + |
| 113 | + public object SelectedItem |
| 114 | + { |
| 115 | + get |
| 116 | + { |
| 117 | + return GetValue(SelectedItemProperty); |
| 118 | + } |
| 119 | + set |
| 120 | + { |
| 121 | + SetValue(SelectedItemProperty, value); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + void ItemsSourceChanging() |
| 126 | + { |
| 127 | + if (ItemsSource != null) |
| 128 | + _selectedIndex = ItemsSource.IndexOf(SelectedItem); |
| 129 | + } |
| 130 | + |
| 131 | + void ItemsSourceChanged() |
| 132 | + { |
| 133 | + if (ItemsSource == null) return; |
| 134 | + |
| 135 | + this.ColumnDefinitions.Clear(); |
| 136 | + foreach (var item in ItemsSource) |
| 137 | + { |
| 138 | + this.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }); |
| 139 | + } |
| 140 | + |
| 141 | + CreateTabs(); |
| 142 | + } |
| 143 | + |
| 144 | + void SelectedItemChanged() |
| 145 | + { |
| 146 | + |
| 147 | + var selectedIndex = ItemsSource.IndexOf(SelectedItem); |
| 148 | + var pagerIndicators = Children.Cast<StackLayout>().ToList(); |
| 149 | + |
| 150 | + foreach (var pi in pagerIndicators) |
| 151 | + { |
| 152 | + UnselectTab(pi); |
| 153 | + } |
| 154 | + |
| 155 | + if (selectedIndex > -1) |
| 156 | + { |
| 157 | + SelectTab(pagerIndicators[selectedIndex]); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + static void UnselectTab(StackLayout tab) |
| 162 | + { |
| 163 | + tab.Opacity = 0.5; |
| 164 | + } |
| 165 | + |
| 166 | + static void SelectTab(StackLayout tab) |
| 167 | + { |
| 168 | + tab.Opacity = 1.0; |
| 169 | + } |
| 170 | + } |
157 | 171 | } |
158 | 172 |
|
0 commit comments