终端一体化运控平台
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

507 řádky
16 KiB

  1. using BeDesignerSCADA.Common;
  2. using BeDesignerSCADA.View;
  3. using BeDesignerSCADA.ViewModel;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Controls.Primitives;
  13. using System.Windows.Data;
  14. using System.Windows.Forms;
  15. using System.Windows.Input;
  16. using System.Windows.Markup;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Animation;
  19. namespace BeDesignerSCADA.Controls
  20. {
  21. /// <summary>
  22. /// MainCanvasPanel.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class MainCanvasPanel : System.Windows.Controls.UserControl
  25. {
  26. /// <summary>
  27. /// 布局文件路径
  28. /// </summary>
  29. public string Path = string.Empty;
  30. List<Type> controls = new List<Type>();
  31. MainViewModelNew viewModel = new MainViewModelNew();
  32. public MainCanvasPanel(string _Path)
  33. {
  34. InitializeComponent();
  35. EditorHelper.Register<BindingExpression, BindingConvertor>();
  36. Path = _Path;
  37. viewModel.LayoutsPath=Path;
  38. this.DataContext = viewModel;
  39. viewModel.Loaded(cav, runCanvas);
  40. //控件加载
  41. Assembly assembly = Assembly.LoadFile($"{System.AppDomain.CurrentDomain.BaseDirectory}\\BPASmartClient.SCADAControl.dll"); //Assembly.GetExecutingAssembly();
  42. controls = assembly.GetTypes().Where(t => t.GetInterface("IExecutable") != null).OrderBy(o => o.Name)?.ToList();
  43. //controls.Add(new Line().GetType());
  44. CtlList.ItemsSource = controls;
  45. //读取文件
  46. FileRead(_Path);
  47. }
  48. #region 位置调整
  49. /// <summary>
  50. /// 左对齐
  51. /// </summary>
  52. /// <param name="sender"></param>
  53. /// <param name="e"></param>
  54. private void AglinLeftBtn_Click(object sender, RoutedEventArgs e)
  55. {
  56. cav.AlignLeft();
  57. }
  58. /// <summary>
  59. /// 底部对齐
  60. /// </summary>
  61. /// <param name="sender"></param>
  62. /// <param name="e"></param>
  63. private void AglinBottomBtn_Click(object sender, RoutedEventArgs e)
  64. {
  65. cav.AlignBottom();
  66. }
  67. /// <summary>
  68. /// 顶部对齐
  69. /// </summary>
  70. /// <param name="sender"></param>
  71. /// <param name="e"></param>
  72. private void AglinTopBtn_Click(object sender, RoutedEventArgs e)
  73. {
  74. cav.AlignTop();
  75. }
  76. /// <summary>
  77. /// 右对齐
  78. /// </summary>
  79. /// <param name="sender"></param>
  80. /// <param name="e"></param>
  81. private void AglinRightBtn_Click(object sender, RoutedEventArgs e)
  82. {
  83. cav.AlignRight();
  84. }
  85. /// <summary>
  86. /// 居中
  87. /// </summary>
  88. /// <param name="sender"></param>
  89. /// <param name="e"></param>
  90. private void AglinCenterBtn_Click(object sender, RoutedEventArgs e)
  91. {
  92. cav.AlignCenter();
  93. }
  94. /// <summary>
  95. /// 垂直分布
  96. /// </summary>
  97. /// <param name="sender"></param>
  98. /// <param name="e"></param>
  99. private void VerticalLayoutBtn_Click(object sender, RoutedEventArgs e)
  100. {
  101. cav.VertialLayout();
  102. }
  103. /// <summary>
  104. /// 水平分布
  105. /// </summary>
  106. /// <param name="sender"></param>
  107. /// <param name="e"></param>
  108. private void HorizontalLayoutBtn_Click(object sender, RoutedEventArgs e)
  109. {
  110. cav.HorizontalLayout();
  111. }
  112. #endregion
  113. #region 其他事件操作
  114. /// <summary>
  115. /// 传入变量管理器地址
  116. /// </summary>
  117. /// <param name="path"></param>
  118. private void LoadBtnValue_Click(object sender, RoutedEventArgs e)
  119. {
  120. OpenFileDialog ofd = new OpenFileDialog();
  121. ofd.Filter = "变量管理器文件|*.json";
  122. if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  123. {
  124. viewModel.VariablePath = ofd.FileName;
  125. bool isSucess= DataBusModel.GetInstance().RefreshVariableManager(ofd.FileName);
  126. System.Windows.MessageBox.Show(isSucess ? "加载成功!" : "加载失败!");
  127. }
  128. }
  129. /// <summary>
  130. /// 运行
  131. /// </summary>
  132. /// <param name="sender"></param>
  133. /// <param name="e"></param>
  134. private void RunBtn_Click(object sender, RoutedEventArgs e)
  135. {
  136. if (sender is System.Windows.Controls.Button btn)
  137. {
  138. if (btn.Tag.ToString() == "运行")
  139. {
  140. cav.ClearSelection();
  141. runCanvas.Run(cav.Generator());
  142. }
  143. else if (btn.Tag.ToString() == "停止")
  144. {
  145. runCanvas.Destory();
  146. }
  147. }
  148. }
  149. /// <summary>
  150. /// 模拟运行
  151. /// </summary>
  152. /// <param name="sender"></param>
  153. /// <param name="e"></param>
  154. private void MNRunBtn_Click(object sender, RoutedEventArgs e)
  155. {
  156. cav.ClearSelection();
  157. RunWindowsLao runWindows = new RunWindowsLao();
  158. runWindows.LoadingData(cav.Generator());
  159. runWindows.Show();
  160. }
  161. /// <summary>
  162. /// 加载
  163. /// </summary>
  164. /// <param name="sender"></param>
  165. /// <param name="e"></param>
  166. private void LoadBtn_Click(object sender, RoutedEventArgs e)
  167. {
  168. OpenFileDialog ofd = new OpenFileDialog();
  169. ofd.Filter = "布局文件|*.lay";
  170. if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  171. {
  172. cav.Load(ofd.FileName);
  173. }
  174. DoubleAnimation da = new DoubleAnimation(-200, 0, new Duration(TimeSpan.FromMilliseconds(250)));
  175. da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
  176. CanvasTranslate.BeginAnimation(TranslateTransform.XProperty, da);
  177. DoubleAnimation daop = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromMilliseconds(250)));
  178. daop.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
  179. cav.BeginAnimation(OpacityProperty, daop);
  180. }
  181. /// <summary>
  182. /// 读取文件
  183. /// </summary>
  184. /// <param name="path"></param>
  185. public void FileRead(string path)
  186. {
  187. try
  188. {
  189. if (File.Exists(path))
  190. {
  191. cav.Load(path);
  192. DoubleAnimation da = new DoubleAnimation(-200, 0, new Duration(TimeSpan.FromMilliseconds(250)));
  193. da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
  194. CanvasTranslate.BeginAnimation(TranslateTransform.XProperty, da);
  195. DoubleAnimation daop = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromMilliseconds(250)));
  196. daop.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
  197. cav.BeginAnimation(OpacityProperty, daop);
  198. }
  199. }
  200. catch (Exception ex)
  201. {
  202. }
  203. }
  204. /// <summary>
  205. /// 根据流加载数据
  206. /// </summary>
  207. public void LoadFrameworkElement(UIElementCollection frameworks)
  208. {
  209. try
  210. {
  211. cav.Children.Clear();
  212. foreach (FrameworkElement element in frameworks)
  213. {
  214. string xamlText = XamlWriter.Save(element);
  215. FrameworkElement item = XamlReader.Parse(xamlText) as FrameworkElement;
  216. cav.Children.Add(item);
  217. }
  218. cav.SelectedItems?.Clear();
  219. }
  220. catch (Exception ex)
  221. {
  222. throw;
  223. }
  224. }
  225. /// <summary>
  226. /// 获取所有控件
  227. /// </summary>
  228. /// <returns></returns>
  229. public List<FrameworkElement> GetChildren()
  230. {
  231. List<FrameworkElement> frameworks = new List<FrameworkElement>();
  232. foreach (FrameworkElement child in cav.Children)
  233. {
  234. string xamlText = XamlWriter.Save(child);
  235. FrameworkElement item = XamlReader.Parse(xamlText) as FrameworkElement;
  236. frameworks.Add(item);
  237. }
  238. return frameworks;
  239. }
  240. /// <summary>
  241. /// 保存文件
  242. /// </summary>
  243. public void FileSave()
  244. {
  245. SaveBtn_Click(null, null);
  246. }
  247. /// <summary>
  248. /// 传入变量管理器地址
  249. /// </summary>
  250. /// <param name="path"></param>
  251. public void VariableManagerPath(string path)
  252. {
  253. try
  254. {
  255. viewModel.VariablePath = path;
  256. }
  257. catch (Exception ex)
  258. {
  259. }
  260. }
  261. /// <summary>
  262. /// 运行程序
  263. /// </summary>
  264. public void Run()
  265. {
  266. try
  267. {
  268. cav.ClearSelection();
  269. runCanvas.Run(cav.Generator());
  270. }
  271. catch (Exception ex)
  272. {
  273. }
  274. }
  275. /// <summary>
  276. /// 停止运行程序
  277. /// </summary>
  278. public void Destory()
  279. {
  280. try
  281. {
  282. runCanvas.Destory();
  283. }
  284. catch (Exception ex)
  285. {
  286. throw;
  287. }
  288. }
  289. /// <summary>
  290. /// 保存
  291. /// </summary>
  292. /// <param name="sender"></param>
  293. /// <param name="e"></param>
  294. private void SaveBtn_Click(object sender, RoutedEventArgs e)
  295. {
  296. if (File.Exists(Path))
  297. {
  298. string str = cav.Save();
  299. File.WriteAllText(Path, str, Encoding.Unicode);
  300. }
  301. else
  302. {
  303. SaveFileDialog sfd = new SaveFileDialog();
  304. sfd.Filter = "布局文件|*.lay";
  305. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  306. {
  307. string str = cav.Save();
  308. File.WriteAllText(sfd.FileName, str, Encoding.Unicode);
  309. }
  310. }
  311. }
  312. /// <summary>
  313. /// 模拟消息发送
  314. /// </summary>
  315. /// <param name="sender"></param>
  316. /// <param name="e"></param>
  317. private void MNBtn_Click(object sender, RoutedEventArgs e)
  318. {
  319. }
  320. #endregion
  321. #region 左侧控件栏移动
  322. /// <summary>
  323. /// 移动到右侧
  324. /// </summary>
  325. /// <param name="sender"></param>
  326. /// <param name="e"></param>
  327. private void CtlList_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
  328. {
  329. if (CtlList.SelectedItem != null && e.LeftButton == MouseButtonState.Pressed)
  330. {
  331. DragDrop.DoDragDrop(CtlList, CtlList.SelectedItem, System.Windows.DragDropEffects.Copy);
  332. codeEditor.Text = cav.Save();
  333. }
  334. }
  335. /// <summary>
  336. /// 显示代码
  337. /// </summary>
  338. /// <param name="sender"></param>
  339. /// <param name="e"></param>
  340. private void showCode_Click(object sender, RoutedEventArgs e)
  341. {
  342. codeEditor.Text = cav.Save();
  343. }
  344. /// <summary>
  345. /// 编辑
  346. /// </summary>
  347. /// <param name="sender"></param>
  348. /// <param name="e"></param>
  349. private void ToggleButton_Click(object sender, RoutedEventArgs e)
  350. {
  351. try
  352. {
  353. if (sender is ToggleButton)
  354. {
  355. ToggleButton toggle = (ToggleButton)sender;
  356. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  357. if (propertyGridCommand != null)
  358. {
  359. viewModel.Edit(propertyGridCommand);
  360. }
  361. }
  362. }
  363. catch (Exception ex)
  364. {
  365. }
  366. }
  367. /// <summary>
  368. /// 路径资源选择
  369. /// </summary>
  370. /// <param name="sender"></param>
  371. /// <param name="e"></param>
  372. private void LJToggleButton_Click(object sender, RoutedEventArgs e)
  373. {
  374. try
  375. {
  376. if (sender is ToggleButton)
  377. {
  378. ToggleButton toggle = (ToggleButton)sender;
  379. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  380. if (propertyGridCommand != null)
  381. {
  382. viewModel.SelectPath(propertyGridCommand);
  383. }
  384. }
  385. }
  386. catch (Exception ex)
  387. {
  388. }
  389. }
  390. /// <summary>
  391. /// 选择进程
  392. /// </summary>
  393. /// <param name="sender"></param>
  394. /// <param name="e"></param>
  395. private void XZToggleButton_Click(object sender, RoutedEventArgs e)
  396. {
  397. try
  398. {
  399. if (sender is ToggleButton)
  400. {
  401. ToggleButton toggle = (ToggleButton)sender;
  402. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  403. if (propertyGridCommand != null)
  404. {
  405. viewModel.SelectValuePath(propertyGridCommand);
  406. }
  407. }
  408. }
  409. catch (Exception ex)
  410. {
  411. }
  412. }
  413. /// <summary>
  414. /// 设置子控件模板
  415. /// </summary>
  416. /// <param name="sender"></param>
  417. /// <param name="e"></param>
  418. private void ChildToggleButton_Click(object sender, RoutedEventArgs e)
  419. {
  420. try
  421. {
  422. if (sender is ToggleButton)
  423. {
  424. ToggleButton toggle = (ToggleButton)sender;
  425. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  426. if (propertyGridCommand != null)
  427. {
  428. viewModel.ChildEdit(propertyGridCommand);
  429. }
  430. }
  431. }
  432. catch (Exception ex)
  433. {
  434. }
  435. }
  436. /// <summary>
  437. /// 数据绑定
  438. /// </summary>
  439. /// <param name="sender"></param>
  440. /// <param name="e"></param>
  441. private void BingToggleButton_Click(object sender, RoutedEventArgs e)
  442. {
  443. try
  444. {
  445. if (sender is ToggleButton)
  446. {
  447. ToggleButton toggle = (ToggleButton)sender;
  448. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  449. if (propertyGridCommand != null)
  450. {
  451. viewModel.BingEdit(propertyGridCommand);
  452. }
  453. }
  454. }
  455. catch (Exception ex)
  456. {
  457. }
  458. }
  459. /// <summary>
  460. /// 清除绑定
  461. /// </summary>
  462. /// <param name="sender"></param>
  463. /// <param name="e"></param>
  464. private void ClearBingToggleButton_Click(object sender, RoutedEventArgs e)
  465. {
  466. try
  467. {
  468. if (sender is ToggleButton)
  469. {
  470. ToggleButton toggle = (ToggleButton)sender;
  471. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  472. if (propertyGridCommand != null)
  473. {
  474. viewModel.ClearBingEdit(propertyGridCommand);
  475. }
  476. }
  477. }
  478. catch (Exception ex)
  479. {
  480. }
  481. }
  482. #endregion
  483. }
  484. }