/DeepForest.Phone.Assets/Shell/ApplicationBar/ApplicationBarMenuItem.cs

http://shoppinganalytics.codeplex.com · C# · 203 lines · 163 code · 36 blank · 4 comment · 9 complexity · 1c9324fb787f5fbfa9a76f28e9863fbb MD5 · raw file

  1. using System;
  2. using System.Net;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Documents;
  6. using System.Windows.Ink;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Animation;
  10. using System.Windows.Shapes;
  11. namespace DeepForest.Phone.Assets.Shell
  12. {
  13. public class ApplicationBarMenuItem : FrameworkElement, Microsoft.Phone.Shell.IApplicationBarMenuItem
  14. {
  15. #region Fields
  16. private readonly Microsoft.Phone.Shell.IApplicationBarMenuItem _sysAppBarMenuItem;
  17. private bool _isAttached;
  18. #endregion
  19. #region Ctor
  20. public ApplicationBarMenuItem()
  21. {
  22. _sysAppBarMenuItem = CreateApplicationBarMenuItem();
  23. _sysAppBarMenuItem.Click += sysAppBarMenuItem_Click;
  24. }
  25. #endregion
  26. #region Properties
  27. internal bool IsAttached
  28. {
  29. get { return _isAttached; }
  30. }
  31. protected Microsoft.Phone.Shell.IApplicationBarMenuItem SysAppBarMenuItem
  32. {
  33. get { return _sysAppBarMenuItem; }
  34. }
  35. #endregion
  36. #region Dependency Properties
  37. #region IsEnabled
  38. public bool IsEnabled
  39. {
  40. get { return (bool)GetValue(IsEnabledProperty); }
  41. set { SetValue(IsEnabledProperty, value); }
  42. }
  43. // Using a DependencyProperty as the backing store for IsEnabled. This enables animation, styling, binding, etc...
  44. public static readonly DependencyProperty IsEnabledProperty =
  45. DependencyProperty.Register(
  46. "IsEnabled",
  47. typeof(bool),
  48. typeof(ApplicationBarMenuItem),
  49. new PropertyMetadata(default(bool), (d, e) => ((ApplicationBarMenuItem)d).IsEnabledChanged((bool)e.NewValue)));
  50. private void IsEnabledChanged(bool isEnabled)
  51. {
  52. SysAppBarMenuItem.IsEnabled = isEnabled;
  53. }
  54. #endregion
  55. #region Text
  56. public string Text
  57. {
  58. get { return (string)GetValue(TextProperty); }
  59. set { SetValue(TextProperty, value); }
  60. }
  61. // Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
  62. public static readonly DependencyProperty TextProperty =
  63. DependencyProperty.Register(
  64. "Text",
  65. typeof(string),
  66. typeof(ApplicationBarMenuItem),
  67. new PropertyMetadata(default(string), (d, e) => ((ApplicationBarMenuItem)d).TextChanged((string)e.NewValue)));
  68. private void TextChanged(string text)
  69. {
  70. SysAppBarMenuItem.Text = text;
  71. }
  72. #endregion
  73. #region Command
  74. public ICommand Command
  75. {
  76. get { return (ICommand)GetValue(CommandProperty); }
  77. set { SetValue(CommandProperty, value); }
  78. }
  79. // Using a DependencyProperty as the backing store for Command. This enables animation, styling, binding, etc...
  80. public static readonly DependencyProperty CommandProperty =
  81. DependencyProperty.Register(
  82. "Command",
  83. typeof(ICommand),
  84. typeof(ApplicationBarMenuItem),
  85. new PropertyMetadata(default(ICommand), (d, e) => ((ApplicationBarMenuItem)d).CommandChanged((ICommand)e.OldValue, (ICommand)e.NewValue)));
  86. private void CommandChanged(ICommand oldCommand, ICommand newCommand)
  87. {
  88. if (oldCommand != null)
  89. {
  90. oldCommand.CanExecuteChanged -= command_CanExecuteChanged;
  91. }
  92. if (newCommand != null)
  93. {
  94. newCommand.CanExecuteChanged += command_CanExecuteChanged;
  95. }
  96. }
  97. private void command_CanExecuteChanged(object sender, EventArgs e)
  98. {
  99. var command = sender as ICommand;
  100. IsEnabled = command.CanExecute(CommandParameter);
  101. }
  102. #endregion
  103. #region CommandParameter
  104. public object CommandParameter
  105. {
  106. get { return (object)GetValue(CommandParameterProperty); }
  107. set { SetValue(CommandParameterProperty, value); }
  108. }
  109. // Using a DependencyProperty as the backing store for CommandParameter. This enables animation, styling, binding, etc...
  110. public static readonly DependencyProperty CommandParameterProperty =
  111. DependencyProperty.Register(
  112. "CommandParameter",
  113. typeof(object),
  114. typeof(ApplicationBarMenuItem),
  115. new PropertyMetadata(null));
  116. #endregion
  117. #endregion
  118. #region Internals
  119. internal void Attach(Microsoft.Phone.Shell.IApplicationBar sysAppBar)
  120. {
  121. if (!_isAttached)
  122. {
  123. OnAttach(sysAppBar);
  124. _isAttached = true;
  125. }
  126. }
  127. internal void Dettach(Microsoft.Phone.Shell.IApplicationBar sysAppBar)
  128. {
  129. if (_isAttached)
  130. {
  131. OnDettach(sysAppBar);
  132. _isAttached = false;
  133. }
  134. }
  135. #endregion
  136. #region Events
  137. public event EventHandler Click
  138. {
  139. add { _sysAppBarMenuItem.Click += value; }
  140. remove { _sysAppBarMenuItem.Click -= value; }
  141. }
  142. #endregion
  143. #region Event Handlers
  144. private void sysAppBarMenuItem_Click(object sender, EventArgs e)
  145. {
  146. if (Command != null && Command.CanExecute(CommandParameter))
  147. {
  148. Command.Execute(CommandParameter);
  149. }
  150. }
  151. #endregion
  152. #region Virtuals
  153. protected virtual Microsoft.Phone.Shell.IApplicationBarMenuItem CreateApplicationBarMenuItem()
  154. {
  155. return new Microsoft.Phone.Shell.ApplicationBarMenuItem();
  156. }
  157. protected virtual void OnAttach(Microsoft.Phone.Shell.IApplicationBar sysAppBar)
  158. {
  159. sysAppBar.MenuItems.Add(_sysAppBarMenuItem);
  160. }
  161. protected virtual void OnDettach(Microsoft.Phone.Shell.IApplicationBar sysAppBar)
  162. {
  163. sysAppBar.MenuItems.Remove(_sysAppBarMenuItem);
  164. }
  165. #endregion
  166. }
  167. }