This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 570
/
RadioButtonManager.cs
134 lines (113 loc) · 4.16 KB
/
RadioButtonManager.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
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Xamarin.Forms;
namespace Xamarin.FormsBook.Toolkit
{
public class RadioButtonManager<T> : BindableObject where T : struct
{
StringBuilder stringBuilder = new StringBuilder();
public static readonly BindableProperty SelectedValueProperty =
BindableProperty.Create(
"SelectedValue",
typeof(T),
typeof(RadioButtonManager<T>),
default(T),
BindingMode.TwoWay,
propertyChanged: (bindable, oldValue, newValue) =>
{
foreach (RadioButtonItem<T> item in ((RadioButtonManager<T>)bindable).Items)
{
item.IsSelected = item.Value.Equals((T)newValue);
}
});
public RadioButtonManager()
{
/*
if (!typeof(T).GetTypeInfo().IsEnum)
{
throw new ArgumentException("T must be an enumerated type");
}
string[] names = Enum.GetNames(typeof(T));
T[] values = (T[])Enum.GetValues(typeof(T));
Items = new List<RadioButtonItem<T>>();
*/
string[] names = null;
T[] values = null;
if (typeof(T).GetTypeInfo().IsEnum)
{
names = Enum.GetNames(typeof(T));
values = (T[])Enum.GetValues(typeof(T));
}
else
{
List<string> namesList = new List<string>();
List<T> valuesList = new List<T>();
// Fields
IEnumerable<FieldInfo> fields = typeof(T).GetTypeInfo().DeclaredFields;
foreach (FieldInfo field in fields)
{
if (field.IsPublic && field.IsStatic && field.FieldType == typeof(T))
{
namesList.Add(field.Name);
valuesList.Add((T)(field.GetValue(null)));
}
}
// Properties
IEnumerable<PropertyInfo> properties = typeof(T).GetTypeInfo().DeclaredProperties;
foreach (PropertyInfo property in properties)
{
MethodInfo method = property.GetMethod;
if (method.IsPublic && method.IsStatic && method.ReturnType == typeof(T) && property.CanRead) // ????
{
namesList.Add(method.Name);
valuesList.Add((T)(property.GetValue(null)));
}
}
// Convert to arrays.
names = namesList.ToArray();
values = valuesList.ToArray();
}
Items = new List<RadioButtonItem<T>>();
for (int i = 0; i < names.Length; i++)
{
RadioButtonItem<T> item =
new RadioButtonItem<T>(BreakUpCamelCase(names[i]),
values[i],
new Command<T>(TapCommand),
values[i].Equals(SelectedValue));
Items.Add(item);
}
}
string BreakUpCamelCase(string name)
{
stringBuilder.Clear();
int index = 0;
foreach (char ch in name)
{
if (index != 0 && Char.IsUpper(ch))
{
stringBuilder.Append(' ');
}
stringBuilder.Append(ch);
index++;
}
return stringBuilder.ToString();
}
public T SelectedValue
{
set { SetValue(SelectedValueProperty, value); }
get { return (T)GetValue(SelectedValueProperty); }
}
public IList<RadioButtonItem<T>> Items
{
private set;
get;
}
void TapCommand(T param)
{
SelectedValue = param;
}
}
}