-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathStates.xaml
334 lines (287 loc) · 11 KB
/
States.xaml
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:m="clr-namespace:Manuela;assembly=Manuela"
xmlns:custom="clr-namespace:Gallery.Views.CustomStates"
x:Class="Gallery.Views.States">
<VerticalStackLayout>
<Label
Style="{StaticResource Headline}"
Text="States"/>
<Label
Text="Manuela states simplify the required xaml to add responsiveness to our UI."/>
<Label
Margin="0, 20"
Text="Property triggers"
Style="{StaticResource SubHeadline}" />
<Label
Text="There are multiple pre-defined property triggers, Disabled, Focused, Hovered, Pressed, Selected, Checked and UnChecked."/>
<Entry
Margin="0, 10"
m:Has.States="{m:States
Focused={m:Set Background=Secondary},
Hovered={m:Set Background=Primary},
Disabled={m:Set Background='Gray,Swatch300'}}"/>
<Editor Style="{StaticResource Code}">
<Editor.Text>
<Entry
m:Has.States="{m:States
Focused={m:Set Background=Secondary},
Hovered={m:Set Background=Primary},
Disabled={m:Set Background='Gray,Swatch300'}}"/>
</Editor.Text>
</Editor>
<Button
Margin="0,10"
Text="Click me!"
m:Has.States="{m:States
Default={m:Set Background=Primary},
Hovered={m:Set Background='Primary,Swatch200', TextColor='Gray,Swatch950'},
Pressed={m:Set Background='Primary,Swatch900', TextColor='Gray,Swatch200'}}"/>
<Editor Style="{StaticResource Code}">
<Editor.Text>
<Button
m:Has.States="{m:States
Default={m:Set Background=Primary},
Hovered={m:Set Background='Primary,Swatch200', TextColor='Gray,Swatch950'},
Pressed={m:Set Background='Primary,Swatch900', TextColor='Gray,Swatch200'}}"/>
</Editor.Text>
</Editor>
<CheckBox
Margin="0,10"
m:Has.States="{m:States
Checked={m:Set Background=Secondary}}"/>
<Editor Style="{StaticResource Code}">
<Editor.Text>
<CheckBox
m:Has.States="{m:States
Checked={m:Set Background=Secondary}}"/>
</Editor.Text>
</Editor>
<CollectionView
Margin="0,10"
SelectionMode="Single">
<CollectionView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Item 1</x:String>
<x:String>Item 2</x:String>
<x:String>Item 3</x:String>
</x:Array>
</CollectionView.ItemsSource>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<Label
Text="{Binding .}"
m:Has.States="{m:States
Selected={m:Set Background=Secondary}}"/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Editor Style="{StaticResource Code}">
<Editor.Text>
<CollectionView>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<Label
m:Has.States="{m:States
Selected={m:Set Background=Secondary}}"/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</Editor.Text>
</Editor>
<Label
Margin="0, 20"
Text="States using a style"
Style="{StaticResource SubHeadline}" />
<VerticalStackLayout>
<VerticalStackLayout.Resources>
<Style TargetType="Entry">
<Setter Property="Background" Value="{m:Brush 'Primary,Swatch300'}"/>
<Setter Property="m:Has.States">
<Setter.Value>
<m:StatesCollection>
<m:Hovered Setters="{m:Set Background='Primary,Swatch300'}" />
<m:Pressed Setters="{m:Set Background='Primary,Swatch600'}" />
<m:Disabled Setters="{m:Set Background= 'Primary,Swatch100'}"/>
</m:StatesCollection>
</Setter.Value>
</Setter>
</Style>
</VerticalStackLayout.Resources>
<Entry Text="Press or hover me" />
<Entry Text="Press or hover me" IsEnabled="False" />
<Entry Text="Press or hover me" />
<Entry Text="Press or hover me" />
</VerticalStackLayout>
<Editor Style="{StaticResource Code}">
<Editor.Text>
<Style TargetType="Entry">
<Setter Property="Background" Value="{m:Brush 'Primary,Swatch300'}"/>
<Setter Property="m:Has.States">
<Setter.Value>
<m:StatesCollection>
<m:Hovered Setters="{m:Set Background='Primary,Swatch300'}" />
<m:Pressed Setters="{m:Set Background='Primary,Swatch600'}" />
<m:Disabled Setters="{m:Set Background= 'Primary,Swatch100'}"/>
</m:StatesCollection>
</Setter.Value>
</Setter>
</Style>
</Editor.Text>
</Editor>
<Label
Margin="0, 20"
Text="Adaptive / Responsive triggers"
Style="{StaticResource SubHeadline}" />
<Label
Margin="0, 20"
Text="Useful to share the UI between mutiple screen sizes, the menu on this app uses this feature, there are 4 sizes:" />
<Label Text="Xs < 576 device-independent units" />
<Label Text="Sm >= 576 device-independent units" />
<Label Text="Md >= 768 device-independent units" />
<Label Text="Lg >= 1024 device-independent units" />
<Label Text="Xl >= 1536 device-independent units" />
<Button
Margin="0, 10"
Text="This button changes background depending on the screen size"
m:Has.States="{m:States
OnXs= {m:Set Background=Primary},
OnSm= {m:Set Background=Secondary},
OnMd= {m:Set Background=Tertiary},
OnXl= {m:Set Background=Gray},
OnXxl= {m:Set Background=Gradient}}">
</Button>
<Editor Style="{StaticResource Code}">
<Editor.Text>
<Button
m:Has.States="{m:States
OnXs= {m:Set Background=Primary},
OnSm= {m:Set Background=Secondary},
OnMd= {m:Set Background=Tertiary},
OnXl= {m:Set Background=Gray},
OnXxl= {m:Set Background=Gradient}}">
</Button>
</Editor.Text>
</Editor>
<Label
Margin="0, 20"
Text="Named states"
Style="{StaticResource SubHeadline}" />
<Label Margin="0,0,0,10" Text="here, we define a state with the name 'active' then we toggle it when the button is clicked."/>
<Button
Text="Toggle"
Clicked="ToggleState">
<m:Has.States>
<m:StatesCollection>
<m:State Name="active" Setters="{m:Set Background=Secondary}"/>
</m:StatesCollection>
</m:Has.States>
</Button>
<Editor Style="{StaticResource Code}">
<Editor.Text>
<Button
Clicked="ToggleState">
<m:Has.States>
<m:StatesCollection>
<m:State Name="active" Setters="{m:Set Background=Secondary}"/>
</m:StatesCollection>
</m:Has.States>
</Button>
// code behind:
private void ToggleState(object sender, EventArgs e)
{
var button = (Button)sender;
var state = button.GetCustomState();
if (state == "active")
{
button.SetCustomState(null);
}
else
{
button.SetCustomState("active");
}
}
</Editor.Text>
</Editor>
<Label
Margin="0, 20"
Text="Data triggers / Custom triggers"
Style="{StaticResource SubHeadline}" />
<Label Text="You can also create your own 'states', the source code of the next examples is located at Views/CustomStates folder." />
<Label
Margin="0, 20"
Text="Disable the button when the entry text is empty." />
<Entry x:Name="entry" Placeholder="Enter text" />
<Button
Text="Save">
<m:Has.States>
<m:StatesCollection>
<custom:IsEmpty Entry="{x:Reference entry}" Setters="{m:Set IsEnabled=False}"/>
</m:StatesCollection>
</m:Has.States>
</Button>
<Editor Style="{StaticResource Code}">
<Editor.Text>
<Button Text="Save">
<m:Has.States>
<m:StatesCollection>
<custom:IsEmpty
Entry="{x:Reference entry}"
Setters="{m:Set IsEnabled=False}"/>
</m:StatesCollection>
</m:Has.States>
</Button>
public partial class IsEmpty : XamlState
{
public Entry? Entry { get; set; }
public override bool IsActive(VisualElement visualElement)
{
return string.IsNullOrEmpty(Entry?.Text);
}
}
</Editor.Text>
</Editor>
<Label
Margin="0, 20"
Text="In the next trigger we evaluate if any entry child text is empty, if any is empty the button is disabled." />
<VerticalStackLayout x:Name="layout">
<Entry Placeholder="Enter mail" />
<Entry Placeholder="Enter phone" />
<Button
Text="Save">
<m:Has.States>
<m:StatesCollection>
<custom:IsAnyChildEmpty
Layout="{x:Reference layout}"
Setters="{m:Set IsEnabled=False}"/>
</m:StatesCollection>
</m:Has.States>
</Button>
</VerticalStackLayout>
<Editor Style="{StaticResource Code}">
<Editor.Text>
<Button Text="Save">
<m:Has.States>
<m:StatesCollection>
<custom:IsAnyChildEmpty
Layout="{x:Reference layout}"
Setters="{m:Set IsEnabled=False}"/>
</m:StatesCollection>
</m:Has.States>
</Button>
public partial class IsAnyChildEmpty : XamlState
{
public VerticalStackLayout Layout { get; set; }
public override bool IsActive(VisualElement visualElement)
{
return Layout.Children
.OfType<Entry>()
.Listen(x => x.Text) // ensures that the state is re-evaluated when any child Text changes.
.Any(x => string.IsNullOrEmpty(x.Text));
}
}
</Editor.Text>
</Editor>
</VerticalStackLayout>
</ContentView>