VibrationControl.xaml.cs 7.1 KB

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