终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

327 lines
9.8 KiB

  1. using BeDesignerSCADA.Common;
  2. using BeDesignerSCADA.Controls;
  3. using BeDesignerSCADA.View;
  4. using BPASmart.Model;
  5. using BPASmartClient.Compiler;
  6. using BPASmartClient.DATABUS;
  7. using BPASmartClient.MessageName;
  8. using BPASmartClient.MessageName.EnumHelp;
  9. using BPASmartClient.MessageName.发送消息Model;
  10. using BPASmartClient.MessageName.接收消息Model;
  11. using ICSharpCode.AvalonEdit;
  12. using Microsoft.Toolkit.Mvvm.ComponentModel;
  13. using Microsoft.Toolkit.Mvvm.Input;
  14. using Microsoft.Win32;
  15. using System;
  16. using System.Collections.Concurrent;
  17. using System.Collections.Generic;
  18. using System.Collections.ObjectModel;
  19. using System.ComponentModel;
  20. using System.Linq;
  21. using System.Reflection;
  22. using System.Text;
  23. using System.Threading.Tasks;
  24. using System.Windows;
  25. using System.Windows.Controls;
  26. using System.Windows.Data;
  27. using System.Windows.Input;
  28. using Xceed.Wpf.Toolkit.PropertyGrid.Attributes;
  29. namespace BeDesignerSCADA.ViewModel
  30. {
  31. public class MainViewModelNew : ObservableObject
  32. {
  33. public MainViewModelNew()
  34. {
  35. IsRunning = false;
  36. //启动 或者 停止
  37. RunUiCommand = new RelayCommand(() =>
  38. {
  39. IsRunning = !IsRunning;
  40. });
  41. }
  42. #region 变量
  43. /// <summary>
  44. /// 变量管理器地址
  45. /// </summary>
  46. public string VariablePath = string.Empty;
  47. /// <summary>
  48. /// 是否正在运行状态
  49. /// </summary>
  50. private bool _IsRunning = false;
  51. public bool IsRunning
  52. {
  53. get
  54. {
  55. return _IsRunning;
  56. }
  57. set
  58. {
  59. _IsRunning = value;
  60. if (value)
  61. {
  62. RunCanvasVisibility = Visibility.Visible;
  63. CanvasPanelVisibility = Visibility.Hidden;
  64. }
  65. else
  66. {
  67. RunCanvasVisibility = Visibility.Hidden;
  68. CanvasPanelVisibility = Visibility.Visible;
  69. }
  70. OnPropertyChanged("IsRunning");
  71. }
  72. }
  73. /// <summary>
  74. /// 画布是否显示
  75. /// </summary>
  76. private Visibility _CanvasPanelVisibility;
  77. public Visibility CanvasPanelVisibility
  78. {
  79. get
  80. {
  81. return _CanvasPanelVisibility;
  82. }
  83. set
  84. {
  85. if (_CanvasPanelVisibility == value)
  86. return;
  87. _CanvasPanelVisibility = value;
  88. OnPropertyChanged("CanvasPanelVisibility");
  89. }
  90. }
  91. /// <summary>
  92. /// 运行代码是否显示
  93. /// </summary>
  94. private Visibility _RunCanvasVisibility;
  95. public Visibility RunCanvasVisibility
  96. {
  97. get
  98. {
  99. return _RunCanvasVisibility;
  100. }
  101. set
  102. {
  103. if (_RunCanvasVisibility == value)
  104. return;
  105. _RunCanvasVisibility = value;
  106. OnPropertyChanged("RunCanvasVisibility");
  107. }
  108. }
  109. /// <summary>
  110. /// 当前代码Xaml
  111. /// </summary>
  112. private string _XamlCode;
  113. public string XamlCode
  114. {
  115. get
  116. {
  117. return _XamlCode;
  118. }
  119. set
  120. {
  121. if (_XamlCode == value)
  122. return;
  123. _XamlCode = value;
  124. OnPropertyChanged("XamlCode");
  125. }
  126. }
  127. /// <summary>
  128. /// 选中控件
  129. /// </summary>
  130. private FrameworkElement _CanSelectedItem;
  131. public FrameworkElement CanSelectedItem
  132. {
  133. get
  134. {
  135. return _CanSelectedItem;
  136. }
  137. set
  138. {
  139. if (_CanSelectedItem == value)
  140. return;
  141. _CanSelectedItem = value;
  142. OnPropertyChanged("CanSelectedItem");
  143. }
  144. }
  145. /// <summary>
  146. /// 设备名称集合
  147. /// </summary>
  148. private ObservableCollection<string> _DevNameList;
  149. public ObservableCollection<string> DevNameList
  150. {
  151. get
  152. {
  153. return _DevNameList;
  154. }
  155. set
  156. {
  157. _DevNameList = value;
  158. OnPropertyChanged("DevNameList");
  159. }
  160. }
  161. /// <summary>
  162. /// 设备变量集合
  163. /// </summary>
  164. private ObservableCollection<string> _DevValueList;
  165. public ObservableCollection<string> DevValueList
  166. {
  167. get
  168. {
  169. return _DevValueList;
  170. }
  171. set
  172. {
  173. _DevValueList = value;
  174. OnPropertyChanged("DevValueList");
  175. }
  176. }
  177. #endregion
  178. #region 命令
  179. /// <summary>
  180. /// 启动或者停止
  181. /// </summary>
  182. public RelayCommand RunUiCommand { get; set; }
  183. /// <summary>
  184. /// 编辑
  185. /// </summary>
  186. public RelayCommand EditCommand { get; set; }
  187. #endregion
  188. #region 常用函数
  189. /// <summary>
  190. /// 显示当前xaml代码
  191. /// </summary>
  192. public void ShowCode(TextEditor textEditor)
  193. {
  194. textEditor.Text = canvasPanel.Save();
  195. }
  196. #endregion
  197. #region 脚本编辑数据
  198. /// <summary>
  199. /// 当前编辑界面
  200. /// </summary>
  201. public CanvasPanel canvasPanel;
  202. /// <summary>
  203. /// 当前运行界面
  204. /// </summary>
  205. public RunCanvas runCanvas;
  206. /// <summary>
  207. /// 加载
  208. /// </summary>
  209. /// <param name="obj"></param>
  210. public void Loaded(object objCan, object objRun)
  211. {
  212. canvasPanel = objCan as CanvasPanel;
  213. runCanvas = objRun as RunCanvas;
  214. }
  215. /// <summary>
  216. /// 编辑
  217. /// </summary>
  218. /// <param name="obj"></param>
  219. public void Edit(object obj)
  220. {
  221. string result = JsEditWindow.ShowEdit(((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value?.ToString(), canvasPanel.GetAllExecChildren());
  222. ((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = result;
  223. }
  224. /// <summary>
  225. /// 子控件编辑
  226. /// </summary>
  227. /// <param name="obj"></param>
  228. public void ChildEdit(object obj)
  229. {
  230. string xml = ChildEditWindow.ShowEdit(((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value?.ToString());
  231. ((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = xml;
  232. }
  233. /// <summary>
  234. /// 绑定编辑
  235. /// </summary>
  236. /// <param name="obj"></param>
  237. public void BingEdit(object obj)
  238. {
  239. //父控件
  240. Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid _property = (Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid)((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).ParentElement;
  241. //选中控件
  242. string controlname = _property.SelectedObjectName;//名称
  243. Type type = _property.SelectedObjectType;//类型
  244. object selectpro = _property.SelectedProperty;
  245. object selectControl= _property.SelectedObject;//对象
  246. string selectProName= _property.SelectedProperty.ToString();
  247. //选中属性
  248. DependencyProperty dependencyProperty = PropertyHelper.GetDependencyProperties(selectControl)?.ToList().Find(x => x.Name == selectProName);
  249. if (dependencyProperty != null)
  250. {
  251. string _binding = PropertyBindingWindow.ShowEdit(dependencyProperty, (selectControl as FrameworkElement));
  252. if (!string.IsNullOrEmpty(_binding))
  253. {
  254. Binding bindingText = new Binding() { Mode = BindingMode.TwoWay, Path = new PropertyPath(_binding) };
  255. (selectControl as FrameworkElement).SetBinding(dependencyProperty, bindingText);
  256. }
  257. else
  258. {
  259. BindingOperations.ClearBinding((selectControl as FrameworkElement), dependencyProperty);
  260. }
  261. }
  262. }
  263. /// <summary>
  264. /// 清除绑定
  265. /// </summary>
  266. /// <param name="obj"></param>
  267. public void ClearBingEdit(object obj)
  268. {
  269. //父控件
  270. Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid _property = (Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid)((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).ParentElement;
  271. //选中控件
  272. string controlname = _property.SelectedObjectName;//名称
  273. Type type = _property.SelectedObjectType;//类型
  274. object selectpro = _property.SelectedProperty;
  275. object selectControl = _property.SelectedObject;//对象
  276. string selectProName = _property.SelectedProperty.ToString();
  277. //选中属性
  278. DependencyProperty dependencyProperty = PropertyHelper.GetDependencyProperties(selectControl)?.ToList().Find(x => x.Name == selectProName);
  279. if (dependencyProperty != null)
  280. {
  281. BindingOperations.ClearBinding((selectControl as FrameworkElement), dependencyProperty);
  282. }
  283. }
  284. /// <summary>
  285. /// 图片选择路径
  286. /// </summary>
  287. /// <param name="obj"></param>
  288. public void SelectPath(object obj)
  289. {
  290. OpenFileDialog ofd = new OpenFileDialog();
  291. ofd.Filter = "图片|*.jpg;*.png;*.gif;*.jpeg;*.bmp";
  292. if (ofd.ShowDialog() == true)
  293. {
  294. ((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = ofd.FileName;
  295. }
  296. }
  297. #endregion
  298. }
  299. }