-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
IndicatorViewRenderer.cs
152 lines (130 loc) · 4.06 KB
/
IndicatorViewRenderer.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
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml.Shapes;
using WEllipse = Windows.UI.Xaml.Shapes.Ellipse;
using WRectangle = Windows.UI.Xaml.Shapes.Rectangle;
using WShape = Windows.UI.Xaml.Shapes.Shape;
using WSolidColorBrush = Windows.UI.Xaml.Media.SolidColorBrush;
namespace Xamarin.Forms.Platform.UWP
{
class IndicatorViewRenderer : ViewRenderer<IndicatorView, FrameworkElement>
{
const int DefaultPadding = 4;
WSolidColorBrush _selectedColor;
WSolidColorBrush _fillColor;
ObservableCollection<WShape> _dots;
public IndicatorViewRenderer()
{
AutoPackage = false;
}
protected override void OnElementChanged(ElementChangedEventArgs<IndicatorView> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
if (Control == null)
{
UpdateControl();
}
_fillColor = new WSolidColorBrush(Element.IndicatorColor.ToWindowsColor());
_selectedColor = new WSolidColorBrush(Element.SelectedIndicatorColor.ToWindowsColor());
CreateIndicators();
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.IsOneOf(IndicatorView.IndicatorColorProperty, IndicatorView.SelectedIndicatorColorProperty, IndicatorView.PositionProperty))
UpdateIndicatorsColor();
if (e.IsOneOf(IndicatorView.CountProperty,
IndicatorView.ItemsSourceProperty,
IndicatorView.IndicatorsShapeProperty))
CreateIndicators();
}
void UpdateControl()
{
var control = (Element.IndicatorTemplate != null)
? (FrameworkElement)Element.IndicatorLayout.GetOrCreateRenderer()
: CreateNativeControl();
SetNativeControl(control);
}
FrameworkElement CreateNativeControl()
{
return new ItemsControl
{
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
ItemsPanel = GetItemsPanelTemplate()
};
}
void UpdateIndicatorsColor()
{
if (!(Control is ItemsControl))
return;
_fillColor = new WSolidColorBrush(Element.IndicatorColor.ToWindowsColor());
_selectedColor = new WSolidColorBrush(Element.SelectedIndicatorColor.ToWindowsColor());
var position = Element.Position;
int i = 0;
foreach (var item in (Control as ItemsControl).Items)
{
((WShape)item).Fill = i == position ? _selectedColor : _fillColor;
i++;
}
}
ItemsPanelTemplate GetItemsPanelTemplate()
{
var itemsPanelTemplateXaml =
$@"<ItemsPanelTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<StackPanel Orientation='Horizontal'></StackPanel>
</ItemsPanelTemplate>";
return (ItemsPanelTemplate)XamlReader.Load(itemsPanelTemplateXaml);
}
void CreateIndicators()
{
if (!Element.IsVisible || !(Control is ItemsControl))
return;
var position = Element.Position;
var indicators = new List<WShape>();
if (Element.ItemsSource != null && Element.Count > 0)
{
int i = 0;
foreach (var item in Element.ItemsSource)
{
indicators.Add(CreateIndicator(i, position));
i++;
}
}
_dots = new ObservableCollection<WShape>(indicators);
(Control as ItemsControl).ItemsSource = _dots;
}
WShape CreateIndicator(int i, int position)
{
var indicatorSize = Element.IndicatorSize;
if (Element.IndicatorsShape == IndicatorShape.Circle)
{
return new WEllipse()
{
Fill = i == position ? _selectedColor : _fillColor,
Height = indicatorSize,
Width = indicatorSize,
Margin = new Windows.UI.Xaml.Thickness(DefaultPadding, 0, DefaultPadding, 0)
};
}
else
{
return new WRectangle()
{
Fill = i == position ? _selectedColor : _fillColor,
Height = indicatorSize,
Width = indicatorSize,
Margin = new Windows.UI.Xaml.Thickness(DefaultPadding, 0, DefaultPadding, 0)
};
}
}
}
}