This repository has been archived by the owner on Nov 21, 2017. It is now read-only.
/
ApplicationBarBehavior.cs
369 lines (326 loc) · 14.3 KB
/
ApplicationBarBehavior.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// ****************************************************************************
// <copyright file="ApplicationBarBehavior.cs" company="Pedro Lamas">
// Copyright © Pedro Lamas 2011
// </copyright>
// ****************************************************************************
// <author>Pedro Lamas</author>
// <email>pedrolamas@gmail.com</email>
// <date>17-11-2011</date>
// <project>Cimbalino.Phone.Toolkit</project>
// <web>http://www.pedrolamas.com</web>
// <license>
// See license.txt in this solution or http://www.pedrolamas.com/license_MIT.txt
// </license>
// ****************************************************************************
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
namespace Cimbalino.Phone.Toolkit.Behaviors
{
/// <summary>
/// The behavior that creates a bindable <see cref="Microsoft.Phone.Shell.ApplicationBar" />.
/// </summary>
[System.Windows.Markup.ContentProperty("Buttons")]
public class ApplicationBarBehavior : SafeBehavior<FrameworkElement>
{
internal readonly IApplicationBar InternalApplicationBar;
/// <summary>
/// Initializes a new instance of the <see cref="ApplicationBarBehavior" /> class.
/// </summary>
public ApplicationBarBehavior()
{
InternalApplicationBar = new Microsoft.Phone.Shell.ApplicationBar();
Buttons = new ApplicationBarIconButtonCollection(InternalApplicationBar.Buttons);
MenuItems = new ApplicationBarMenuItemCollection(InternalApplicationBar.MenuItems);
InternalApplicationBar.StateChanged += ApplicationBarStateChanged;
}
/// <summary>
/// Occurs when the user opens or closes the menu.
/// </summary>
public event EventHandler<ApplicationBarStateChangedEventArgs> StateChanged;
/// <summary>
/// Called after the behavior is attached to an AssociatedObject.
/// </summary>
/// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks>
protected override void OnAttached()
{
AssociatedObject.LayoutUpdated += AssociatedObjectLayoutUpdated;
base.OnAttached();
}
/// <summary>
/// Gets the <see cref="ApplicationBarMenuItem"/> collection that appear on the Application Bar.
/// </summary>
/// <value>The <see cref="ApplicationBarMenuItem"/> collection.</value>
[Category("Common")]
public ApplicationBarMenuItemCollection MenuItems
{
get
{
return (ApplicationBarMenuItemCollection)GetValue(MenuItemsProperty);
}
private set
{
SetValue(MenuItemsProperty, value);
}
}
/// <summary>
/// Identifier for the <see cref="MenuItems" /> dependency property.
/// </summary>
public static readonly DependencyProperty MenuItemsProperty =
DependencyProperty.Register("MenuItems", typeof(ApplicationBarMenuItemCollection), typeof(ApplicationBarBehavior), null);
/// <summary>
/// Gets the <see cref="ApplicationBarIconButton"/> collection that appear on the Application Bar.
/// </summary>
/// <value>The <see cref="ApplicationBarIconButton"/> collection.</value>
[Category("Common")]
public ApplicationBarIconButtonCollection Buttons
{
get
{
return (ApplicationBarIconButtonCollection)GetValue(ButtonsProperty);
}
private set
{
SetValue(ButtonsProperty, value);
}
}
/// <summary>
/// Identifier for the <see cref="Buttons" /> dependency property.
/// </summary>
public static readonly DependencyProperty ButtonsProperty =
DependencyProperty.Register("Buttons", typeof(ApplicationBarIconButtonCollection), typeof(ApplicationBarBehavior), null);
/// <summary>
/// Gets or sets the background color of the Application Bar.
/// </summary>
/// <value>The background color of the Application Bar.</value>
[Category("Appearance")]
public Color BackgroundColor
{
get
{
return (Color)GetValue(BackgroundColorProperty);
}
set
{
SetValue(BackgroundColorProperty, value);
}
}
/// <summary>
/// Identifier for the <see cref="BackgroundColor" /> dependency property.
/// </summary>
public static readonly DependencyProperty BackgroundColorProperty =
DependencyProperty.Register("BackgroundColor", typeof(Color), typeof(ApplicationBarBehavior), new PropertyMetadata(OnBackgroundColorChanged));
/// <summary>
/// Called after the background color of the ApplicationBar is changed.
/// </summary>
/// <param name="d">The <see cref="DependencyObject" />.</param>
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs" /> instance containing the event data.</param>
protected static void OnBackgroundColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ApplicationBarBehavior)d).InternalApplicationBar.BackgroundColor = (Color)e.NewValue;
}
/// <summary>
/// Gets or sets the foreground color of the Application Bar.
/// </summary>
/// <value>The foreground color of the Application Bar.</value>
[Category("Appearance")]
public Color ForegroundColor
{
get
{
return (Color)GetValue(ForegroundColorProperty);
}
set
{
SetValue(ForegroundColorProperty, value);
}
}
/// <summary>
/// Identifier for the <see cref="ForegroundColor" /> dependency property.
/// </summary>
public static readonly DependencyProperty ForegroundColorProperty =
DependencyProperty.Register("ForegroundColor", typeof(Color), typeof(ApplicationBarBehavior), new PropertyMetadata(OnForegroundColorChanged));
/// <summary>
/// Called after the foreground color of the ApplicationBar is changed.
/// </summary>
/// <param name="d">The <see cref="DependencyObject" />.</param>
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs" /> instance containing the event data.</param>
protected static void OnForegroundColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ApplicationBarBehavior)d).InternalApplicationBar.ForegroundColor = (Color)e.NewValue;
}
/// <summary>
/// Gets or sets a value indicating whether the user can open the menu.
/// </summary>
/// <value>true if the menu is enabled; otherwise, false.</value>
[Category("Common")]
public bool IsMenuEnabled
{
get
{
return (bool)GetValue(IsMenuEnabledProperty);
}
set
{
SetValue(IsMenuEnabledProperty, value);
}
}
/// <summary>
/// Identifier for the <see cref="IsMenuEnabled" /> dependency property.
/// </summary>
public static readonly DependencyProperty IsMenuEnabledProperty =
DependencyProperty.Register("IsMenuEnabled", typeof(bool), typeof(ApplicationBarBehavior), new PropertyMetadata(true, OnIsMenuEnabledChanged));
/// <summary>
/// Called after the menu enabled state of the Application Bar is changed.
/// </summary>
/// <param name="d">The <see cref="DependencyObject" />.</param>
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs" /> instance containing the event data.</param>
protected static void OnIsMenuEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ApplicationBarBehavior)d).InternalApplicationBar.IsMenuEnabled = (bool)e.NewValue;
}
/// <summary>
/// Gets or sets a value indicating whether the Application Bar is visible.
/// </summary>
/// <value>true if the Application Bar is visible; otherwise, false.</value>
[Category("Appearance")]
public bool IsVisible
{
get
{
return (bool)GetValue(IsVisibleProperty);
}
set
{
SetValue(IsVisibleProperty, value);
}
}
/// <summary>
/// Identifier for the <see cref="IsVisible" /> dependency property.
/// </summary>
public static readonly DependencyProperty IsVisibleProperty =
DependencyProperty.Register("IsVisible", typeof(bool), typeof(ApplicationBarBehavior), new PropertyMetadata(true, OnIsVisibleChanged));
/// <summary>
/// Called after the visible state of the Application Bar is changed.
/// </summary>
/// <param name="d">The <see cref="DependencyObject" />.</param>
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs" /> instance containing the event data.</param>
protected static void OnIsVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ApplicationBarBehavior)d).InternalApplicationBar.IsVisible = (bool)e.NewValue;
}
/// <summary>
/// Gets or sets the size of the Application Bar.
/// </summary>
/// <value>One of the enumeration values that indicates the size of the Application Bar.</value>
public ApplicationBarMode Mode
{
get
{
return (ApplicationBarMode)GetValue(ModeProperty);
}
set
{
SetValue(ModeProperty, value);
}
}
/// <summary>
/// Identifier for the <see cref="Mode" /> dependency property.
/// </summary>
public static readonly DependencyProperty ModeProperty =
DependencyProperty.Register("Mode", typeof(ApplicationBarMode), typeof(ApplicationBarBehavior), new PropertyMetadata(ApplicationBarMode.Default, OnModeChanged));
/// <summary>
/// Called after the size of the ApplicationBar is changed.
/// </summary>
/// <param name="d">The <see cref="DependencyObject" />.</param>
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs" /> instance containing the event data.</param>
protected static void OnModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ApplicationBarBehavior)d).InternalApplicationBar.Mode = (ApplicationBarMode)e.NewValue;
}
/// <summary>
/// Gets or sets the opacity of the Application Bar.
/// </summary>
/// <value>The opacity of the Application Bar.</value>
[Category("Appearance")]
public double Opacity
{
get
{
return (double)GetValue(OpacityProperty);
}
set
{
SetValue(OpacityProperty, value);
}
}
/// <summary>
/// Identifier for the <see cref="Opacity" /> dependency property.
/// </summary>
public static readonly DependencyProperty OpacityProperty =
DependencyProperty.Register("Opacity", typeof(double), typeof(ApplicationBarBehavior), new PropertyMetadata(1.0, OnOpacityChanged));
/// <summary>
/// Called after the opacity of the ApplicationBar is changed.
/// </summary>
/// <param name="d">The <see cref="DependencyObject" />.</param>
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs" /> instance containing the event data.</param>
protected static void OnOpacityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ApplicationBarBehavior)d).InternalApplicationBar.Opacity = (double)e.NewValue;
}
/// <summary>
/// Gets or sets the command to invoke when the user opens or closes the menu. This is a DependencyProperty.
/// </summary>
/// <value>The command to invoke when the user opens or closes the menu. The default is null.</value>
[Category("Common")]
public ICommand StateChangedCommand
{
get { return (ICommand)GetValue(StateChangedCommandProperty); }
set { SetValue(StateChangedCommandProperty, value); }
}
/// <summary>
/// Identifier for the <see cref="StateChangedCommand" /> dependency property.
/// </summary>
public static readonly DependencyProperty StateChangedCommandProperty =
DependencyProperty.Register("StateChangedCommand", typeof(ICommand), typeof(ApplicationBarBehavior), null);
private void AssociatedObjectLayoutUpdated(object sender, EventArgs e)
{
AssociatedObject.LayoutUpdated -= AssociatedObjectLayoutUpdated;
SetApplicationBar();
}
private void ApplicationBarStateChanged(object sender, ApplicationBarStateChangedEventArgs e)
{
var eventHandler = StateChanged;
if (eventHandler != null)
{
eventHandler(sender, e);
}
var command = StateChangedCommand;
if (command != null)
{
var commandParameter = e;
if (command.CanExecute(commandParameter))
{
command.Execute(commandParameter);
}
}
}
private void SetApplicationBar()
{
if (DesignerProperties.IsInDesignTool || AssociatedObject == null)
{
return;
}
var page = AssociatedObject as PhoneApplicationPage ?? AssociatedObject.Parent as PhoneApplicationPage;
if (page == null)
{
throw new Exception("This ApplicationBarBehavior element can only be attached to the Page or LayoutRoot element");
}
page.ApplicationBar = InternalApplicationBar;
}
}
}