VibrationControl.xaml.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using MaterialDesignThemes.Wpf;
  2. using Prism.Commands;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. using System.Runtime.CompilerServices;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Navigation;
  19. using System.Windows.Shapes;
  20. using Team.FFFeederService.Interfaces;
  21. using TeamAAS_VP;
  22. namespace TeamAAS_VP.Controls
  23. {
  24. /// <summary>
  25. /// VibrationControl.xaml 的交互逻辑
  26. /// </summary>
  27. public partial class VibrationControl : UserControl, INotifyPropertyChanged
  28. {
  29. public VibrationControl()
  30. {
  31. InitializeComponent();
  32. this.DataContext = this;
  33. this.Loaded += VibrationControl_Loaded;
  34. }
  35. private async void VibrationControl_Loaded(object sender, RoutedEventArgs e)
  36. {
  37. if (Feeder != null && Feeder.IsConnected)
  38. {
  39. if (await Feeder.QueryLightStateAsync())
  40. {
  41. LightKind = PackIconKind.LightbulbOutline;
  42. }
  43. else
  44. {
  45. LightKind = PackIconKind.LightbulbOnOutline;
  46. }
  47. }
  48. }
  49. #region 属性
  50. public IVoiceCoilMotorFeeder Feeder { get;set; }
  51. private PackIconKind _LightKind= PackIconKind.LightbulbOnOutline;
  52. public PackIconKind LightKind
  53. {
  54. get { return _LightKind; }
  55. set { SetProperty(ref _LightKind,value); }
  56. }
  57. #endregion
  58. #region 命令
  59. private DelegateCommand<string> _ActionCommand;
  60. public DelegateCommand<string> ActionCommand =>
  61. _ActionCommand ?? (_ActionCommand = new DelegateCommand<string>(ExecuteActionCommand));
  62. #endregion
  63. #region 方法
  64. /// <summary>
  65. /// 执行动作命令
  66. /// </summary>
  67. async void ExecuteActionCommand(string action)
  68. {
  69. try
  70. {
  71. if (int.TryParse(action, out int index))
  72. {
  73. if (Feeder == null) return;
  74. if (!Feeder.IsConnected) return;
  75. switch (index)
  76. {
  77. case -1:
  78. if (await Feeder.QueryLightStateAsync())
  79. {
  80. await Feeder.CloseLightAsync(); //关闭光源
  81. LightKind = PackIconKind.LightbulbOutline;
  82. }
  83. else
  84. {
  85. await Feeder.OpenLightAsync(); //打开光源
  86. LightKind = PackIconKind.LightbulbOnOutline;
  87. }
  88. break;
  89. case 0:
  90. case 1:
  91. case 2:
  92. case 3:
  93. case 4:
  94. case 5:
  95. case 6:
  96. case 7:
  97. case 8:
  98. case 9:
  99. case 10:
  100. await Feeder.StartAction(index);
  101. break;
  102. }
  103. }
  104. }
  105. catch (Exception ex)
  106. {
  107. LogHelper.WriteLogError("Feeder执行动作命令时出错!", ex);
  108. }
  109. }
  110. #endregion
  111. #region 属性更改通知
  112. /// <summary>
  113. /// Occurs when a property value changes.
  114. /// </summary>
  115. public event PropertyChangedEventHandler PropertyChanged;
  116. /// <summary>
  117. /// Checks if a property already matches a desired value. Sets the property and
  118. /// notifies listeners only when necessary.
  119. /// </summary>
  120. /// <typeparam name="T">Type of the property.</typeparam>
  121. /// <param name="storage">Reference to a property with both getter and setter.</param>
  122. /// <param name="value">Desired value for the property.</param>
  123. /// <param name="propertyName">Name of the property used to notify listeners. This
  124. /// value is optional and can be provided automatically when invoked from compilers that
  125. /// support CallerMemberName.</param>
  126. /// <returns>True if the value was changed, false if the existing value matched the
  127. /// desired value.</returns>
  128. protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
  129. {
  130. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  131. storage = value;
  132. RaisePropertyChanged(propertyName);
  133. return true;
  134. }
  135. /// <summary>
  136. /// Checks if a property already matches a desired value. Sets the property and
  137. /// notifies listeners only when necessary.
  138. /// </summary>
  139. /// <typeparam name="T">Type of the property.</typeparam>
  140. /// <param name="storage">Reference to a property with both getter and setter.</param>
  141. /// <param name="value">Desired value for the property.</param>
  142. /// <param name="propertyName">Name of the property used to notify listeners. This
  143. /// value is optional and can be provided automatically when invoked from compilers that
  144. /// support CallerMemberName.</param>
  145. /// <param name="onChanged">Action that is called after the property value has been changed.</param>
  146. /// <returns>True if the value was changed, false if the existing value matched the
  147. /// desired value.</returns>
  148. protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
  149. {
  150. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  151. storage = value;
  152. onChanged?.Invoke();
  153. RaisePropertyChanged(propertyName);
  154. return true;
  155. }
  156. /// <summary>
  157. /// Raises this object's PropertyChanged event.
  158. /// </summary>
  159. /// <param name="propertyName">Name of the property used to notify listeners. This
  160. /// value is optional and can be provided automatically when invoked from compilers
  161. /// that support <see cref="CallerMemberNameAttribute"/>.</param>
  162. protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
  163. {
  164. OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  165. }
  166. /// <summary>
  167. /// Raises this object's PropertyChanged event.
  168. /// </summary>
  169. /// <param name="args">The PropertyChangedEventArgs</param>
  170. protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
  171. {
  172. PropertyChanged?.Invoke(this, args);
  173. }
  174. #endregion
  175. }
  176. }