AflexControl.xaml.cs 6.5 KB

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