Pump.xaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. namespace Aitex.Core.UI.Control
  15. {
  16. /// <summary>
  17. /// Interaction logic for Pump.xaml
  18. /// </summary>
  19. public partial class Pump : UserControl
  20. {
  21. public Pump()
  22. {
  23. InitializeComponent();
  24. }
  25. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
  26. "Command", typeof(ICommand), typeof(Pump),
  27. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  28. public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(
  29. "Orientation", typeof(ValveDirection), typeof(Pump),
  30. new FrameworkPropertyMetadata(ValveDirection.ToBottom, FrameworkPropertyMetadataOptions.AffectsRender));
  31. public static readonly DependencyProperty PumpAlarmProperty = DependencyProperty.Register(
  32. "PumpAlarm", typeof(object), typeof(Pump),
  33. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  34. public static readonly DependencyProperty PumpRunningProperty = DependencyProperty.Register(
  35. "PumpRunning", typeof(object), typeof(Pump),
  36. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  37. /// <summary>
  38. /// Command to execute when turning on/off pump.
  39. /// </summary>
  40. public ICommand Command
  41. {
  42. get
  43. {
  44. return (ICommand)this.GetValue(CommandProperty);
  45. }
  46. set
  47. {
  48. this.SetValue(CommandProperty, value);
  49. }
  50. }
  51. public object PumpAlarm
  52. {
  53. get
  54. {
  55. return GetValue(PumpAlarmProperty);
  56. }
  57. set
  58. {
  59. SetValue(PumpAlarmProperty, value);
  60. }
  61. }
  62. public object PumpRunning
  63. {
  64. get
  65. {
  66. return GetValue(PumpRunningProperty);
  67. }
  68. set
  69. {
  70. SetValue(PumpRunningProperty, value);
  71. }
  72. }
  73. /// <summary>
  74. /// direction of the valve
  75. /// </summary>
  76. public ValveDirection Orientation
  77. {
  78. get
  79. {
  80. return (ValveDirection)GetValue(OrientationProperty);
  81. }
  82. set
  83. {
  84. SetValue(OrientationProperty, value);
  85. }
  86. }
  87. private BitmapSource GetImage(string uri)
  88. {
  89. BitmapImage image = new BitmapImage(new Uri(string.Format("pack://application:,,,/MECF.Framework.Common;component/Resources/Valve/{0}", uri)));
  90. return image;
  91. //return BitmapFrame.Create(new Uri(string.Format(@"{0}", uri), UriKind.Relative));
  92. }
  93. int GetPumpState()
  94. {
  95. if ((PumpAlarm + "").ToLower() == "true")
  96. {
  97. //报警红
  98. return 3;
  99. }
  100. else
  101. {
  102. //非报警状态
  103. if ((PumpRunning + "").ToLower() == "true")
  104. {
  105. //runing 绿
  106. return 0;
  107. }
  108. else if ((PumpRunning + "").ToLower() == "false")
  109. {
  110. //关闭黑
  111. return 1;
  112. }
  113. else
  114. {
  115. //返回null,灰色
  116. return 2;
  117. }
  118. }
  119. }
  120. protected override void OnRender(DrawingContext drawingContext)
  121. {
  122. int pumpState = GetPumpState();
  123. switch (pumpState)
  124. {
  125. case 0:
  126. imgPump.Source = GetImage("PumpGreen.png");
  127. break;
  128. case 1: imgPump.Source = GetImage( "PumpBlack.png");
  129. break;
  130. case 2: imgPump.Source = GetImage( "PumpGray.png");
  131. break;
  132. case 3: imgPump.Source = GetImage("PumpRed.png");
  133. break;
  134. default: imgPump.Source = GetImage( "PumpRed.png");
  135. break;
  136. }
  137. switch (Orientation)
  138. {
  139. case ValveDirection.ToBottom:
  140. case ValveDirection.ToTop:
  141. rotateTransform.Angle = 0;
  142. break;
  143. case ValveDirection.ToLeft:
  144. case ValveDirection.ToRight:
  145. rotateTransform.Angle = 90;
  146. break;
  147. default:
  148. break;
  149. }
  150. base.OnRender(drawingContext);
  151. }
  152. /// <summary>
  153. /// open
  154. /// </summary>
  155. /// <param name="sender"></param>
  156. /// <param name="e"></param>
  157. private void OpenPump(object sender, RoutedEventArgs e)
  158. {
  159. MenuItem mi = sender as MenuItem;
  160. Command.Execute(new object[] { this.Tag, "Open"});
  161. }
  162. private void ClosePump(object sender, RoutedEventArgs e)
  163. {
  164. MenuItem mi = sender as MenuItem;
  165. Command.Execute(new object[] { this.Tag, "Close" });
  166. }
  167. }
  168. }