终端一体化运控平台
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

MainViewModelNew.cs 28 KiB

il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. using BeDesignerSCADA.Common;
  2. using BeDesignerSCADA.Controls;
  3. using BeDesignerSCADA.View;
  4. using BPASmartClient.Compiler;
  5. using BPASmartClient.SCADAControl.Converters;
  6. using ICSharpCode.AvalonEdit;
  7. using Microsoft.Toolkit.Mvvm.ComponentModel;
  8. using Microsoft.Toolkit.Mvvm.Input;
  9. using Microsoft.Win32;
  10. using Newtonsoft.Json;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Collections.ObjectModel;
  14. using System.Diagnostics;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Windows;
  18. using System.Windows.Data;
  19. using System.Windows.Markup;
  20. namespace BeDesignerSCADA.ViewModel
  21. {
  22. public class MainViewModelNew : ObservableObject
  23. {
  24. public MainViewModelNew()
  25. {
  26. IsRunning = false;
  27. //启动 或者 停止
  28. RunUiCommand = new RelayCommand(() =>
  29. {
  30. IsRunning = !IsRunning;
  31. });
  32. }
  33. #region 变量
  34. /// <summary>
  35. /// 变量管理器地址
  36. /// </summary>
  37. public string VariablePath = string.Empty;
  38. /// <summary>
  39. /// 布局路径
  40. /// </summary>
  41. public string LayoutsPath = string.Empty;
  42. /// <summary>
  43. /// 是否正在运行状态
  44. /// </summary>
  45. private bool _IsRunning = false;
  46. public bool IsRunning
  47. {
  48. get
  49. {
  50. return _IsRunning;
  51. }
  52. set
  53. {
  54. _IsRunning = value;
  55. if (value)
  56. {
  57. RunCanvasVisibility = Visibility.Visible;
  58. CanvasPanelVisibility = Visibility.Collapsed;
  59. }
  60. else
  61. {
  62. RunCanvasVisibility = Visibility.Collapsed;
  63. CanvasPanelVisibility = Visibility.Visible;
  64. }
  65. OnPropertyChanged("IsRunning");
  66. }
  67. }
  68. /// <summary>
  69. /// 画布是否显示
  70. /// </summary>
  71. private Visibility _CanvasPanelVisibility;
  72. public Visibility CanvasPanelVisibility
  73. {
  74. get
  75. {
  76. return _CanvasPanelVisibility;
  77. }
  78. set
  79. {
  80. if (_CanvasPanelVisibility == value)
  81. return;
  82. _CanvasPanelVisibility = value;
  83. OnPropertyChanged("CanvasPanelVisibility");
  84. }
  85. }
  86. /// <summary>
  87. /// 运行代码是否显示
  88. /// </summary>
  89. private Visibility _RunCanvasVisibility;
  90. public Visibility RunCanvasVisibility
  91. {
  92. get
  93. {
  94. return _RunCanvasVisibility;
  95. }
  96. set
  97. {
  98. if (_RunCanvasVisibility == value)
  99. return;
  100. _RunCanvasVisibility = value;
  101. OnPropertyChanged("RunCanvasVisibility");
  102. }
  103. }
  104. /// <summary>
  105. /// 当前代码Xaml
  106. /// </summary>
  107. private string _XamlCode;
  108. public string XamlCode
  109. {
  110. get
  111. {
  112. return _XamlCode;
  113. }
  114. set
  115. {
  116. if (_XamlCode == value)
  117. return;
  118. _XamlCode = value;
  119. OnPropertyChanged("XamlCode");
  120. }
  121. }
  122. /// <summary>
  123. /// 选中控件
  124. /// </summary>
  125. private FrameworkElement _CanSelectedItem;
  126. public FrameworkElement CanSelectedItem
  127. {
  128. get
  129. {
  130. return _CanSelectedItem;
  131. }
  132. set
  133. {
  134. if (_CanSelectedItem == value)
  135. return;
  136. _CanSelectedItem = value;
  137. OnPropertyChanged("CanSelectedItem");
  138. }
  139. }
  140. /// <summary>
  141. /// 菜单Model
  142. /// </summary>
  143. private MenuModel _MenuModel;
  144. public MenuModel MenuModel
  145. {
  146. get
  147. {
  148. return _MenuModel;
  149. }
  150. set
  151. {
  152. _MenuModel = value;
  153. OnPropertyChanged("MenuModel");
  154. }
  155. }
  156. /// <summary>
  157. /// 控件集合名称映射
  158. /// </summary>
  159. public Dictionary<string, string> ControlsNameValues = new Dictionary<string, string>() {
  160. {"TheButton","按钮" },
  161. {"TheTimer","计时器" },
  162. {"TheCheckBox","勾选框" },
  163. {"DigitalNumber","液晶数字" },
  164. {"TheComboBox","下拉框" },
  165. {"TheImage","图片" },
  166. {"TheTextBlock","文本块" },
  167. {"ArcGauge","仪表盘" },
  168. {"StatusLight","状态灯" },
  169. {"TheToggleButton","开关按钮" },
  170. {"TheGroupBox","分组" },
  171. {"TheTextBox","文本框" },
  172. {"NumberBox","数值框" },
  173. {"GraphArrow","箭头" },
  174. {"GraphStar","五角星" },
  175. {"TheSlider","滑块" },
  176. {"TheRadioButton","单选按钮" },
  177. {"SwitchButton","开关" },
  178. {"KnobButton","旋钮" },
  179. {"Silos","物料仓" },
  180. {"NewConveyorBelt","滚动线" },
  181. {"TheDataGrid","表格" },
  182. {"TheListBox","列表控件" },
  183. {"TheRedis","Redis控件" },
  184. {"WaveProgressBar","进度条波浪" },
  185. {"TheProgressBar","进度条正常" },
  186. {"TheRedProgressBar","进度条圆形红" },
  187. {"TheBlueProgressBar","进度条圆形蓝" },
  188. {"TheGreenProgressBar","进度条圆形绿" },
  189. {"TheAPI","API接口" },
  190. {"TheMQTT","MQTT" },
  191. {"TheTabControl","选项卡控件" },
  192. {"TheCylinder","气缸控件" },
  193. {"TheRectangle","矩形" },
  194. {"TheEllipse","圆形" },
  195. {"TheWuLiaoControl","物料集合控件" },
  196. {"FYFTheListBox","列表控件-拖拽" },
  197. {"ThePopMessage","消息弹窗" },
  198. {"TheMessage","消息提示" },
  199. };
  200. #endregion
  201. #region 命令
  202. /// <summary>
  203. /// 启动或者停止
  204. /// </summary>
  205. public RelayCommand RunUiCommand { get; set; }
  206. /// <summary>
  207. /// 编辑
  208. /// </summary>
  209. public RelayCommand EditCommand { get; set; }
  210. #endregion
  211. #region 常用函数
  212. /// <summary>
  213. /// 显示当前xaml代码
  214. /// </summary>
  215. public void ShowCode(TextEditor textEditor)
  216. {
  217. textEditor.Text = canvasPanel.Save();
  218. }
  219. /// <summary>
  220. /// 更新页面
  221. /// </summary>
  222. public void UpdatePageBase(PageModel _pageModel)
  223. {
  224. try
  225. {
  226. List<string> ChildrenStr = new List<string>();
  227. foreach (FrameworkElement element in _pageModel.visual.Children)
  228. {
  229. string xamlText = XamlWriter.Save(element);
  230. ChildrenStr.Add(xamlText);
  231. }
  232. MenuModel.SelectPageModels.ChildrenStr= ChildrenStr;
  233. }
  234. catch (Exception ex)
  235. {
  236. }
  237. }
  238. #endregion
  239. #region 脚本编辑数据
  240. /// <summary>
  241. /// 当前编辑界面
  242. /// </summary>
  243. public CanvasPanel canvasPanel;
  244. /// <summary>
  245. /// 当前运行界面
  246. /// </summary>
  247. public object runCanvas;
  248. /// <summary>
  249. /// 加载
  250. /// </summary>
  251. /// <param name="obj"></param>
  252. public void Loaded(object objCan, object objRun)
  253. {
  254. canvasPanel = objCan as CanvasPanel;
  255. runCanvas = objRun;// as RunCanvas;
  256. InitCommand();
  257. }
  258. /// <summary>
  259. /// 编辑
  260. /// </summary>
  261. /// <param name="obj"></param>
  262. public void Edit(object obj)
  263. {
  264. string result = JsEditWindow.ShowEdit(((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value?.ToString(), canvasPanel.GetAllExecChildren());
  265. ((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = result;
  266. }
  267. /// <summary>
  268. /// 子控件编辑
  269. /// </summary>
  270. /// <param name="obj"></param>
  271. public void ChildEdit(object obj)
  272. {
  273. string xml = ChildEditWindow.ShowEdit(((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value?.ToString());
  274. ((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = xml;
  275. }
  276. /// <summary>
  277. /// 绑定编辑
  278. /// </summary>
  279. /// <param name="obj"></param>
  280. public void BingEdit(object obj)
  281. {
  282. //父控件
  283. Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid _property = (Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid)((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).ParentElement;
  284. //选中控件
  285. string controlname = _property.SelectedObjectName;//名称
  286. Type type = _property.SelectedObjectType;//类型
  287. object selectpro = _property.SelectedProperty;
  288. object selectControl = _property.SelectedObject;//对象
  289. string selectProName = _property.SelectedProperty.ToString();
  290. //选中属性
  291. DependencyProperty dependencyProperty = PropertyHelper.GetDependencyProperties(selectControl)?.ToList().Find(x => x.Name == selectProName);
  292. if (dependencyProperty != null)
  293. {
  294. string _binding = PropertyBindingWindow.ShowEdit(dependencyProperty, (selectControl as FrameworkElement), VariablePath);
  295. if (!string.IsNullOrEmpty(_binding))
  296. {
  297. Binding bindingText = new Binding()
  298. {
  299. Mode = BindingMode.TwoWay,
  300. Converter = new JTokenToBoolConverter(),
  301. RelativeSource = new RelativeSource { Mode = RelativeSourceMode.Self },
  302. Path = new PropertyPath(_binding)
  303. };
  304. (selectControl as FrameworkElement).SetBinding(dependencyProperty, bindingText);
  305. }
  306. else
  307. {
  308. BindingOperations.ClearBinding((selectControl as FrameworkElement), dependencyProperty);
  309. }
  310. }
  311. }
  312. /// <summary>
  313. /// 清除绑定
  314. /// </summary>
  315. /// <param name="obj"></param>
  316. public void ClearBingEdit(object obj)
  317. {
  318. //父控件
  319. Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid _property = (Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid)((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).ParentElement;
  320. //选中控件
  321. string controlname = _property.SelectedObjectName;//名称
  322. Type type = _property.SelectedObjectType;//类型
  323. object selectpro = _property.SelectedProperty;
  324. object selectControl = _property.SelectedObject;//对象
  325. string selectProName = _property.SelectedProperty.ToString();
  326. //选中属性
  327. DependencyProperty dependencyProperty = PropertyHelper.GetDependencyProperties(selectControl)?.ToList().Find(x => x.Name == selectProName);
  328. if (dependencyProperty != null)
  329. {
  330. BindingOperations.ClearBinding((selectControl as FrameworkElement), dependencyProperty);
  331. }
  332. }
  333. /// <summary>
  334. /// 图片选择路径
  335. /// </summary>
  336. /// <param name="obj"></param>
  337. public void SelectPath(object obj)
  338. {
  339. OpenFileDialog ofd = new OpenFileDialog();
  340. ofd.Filter = "图片|*.jpg;*.png;*.gif;*.jpeg;*.bmp";
  341. if (ofd.ShowDialog() == true)
  342. {
  343. if (!string.IsNullOrEmpty(LayoutsPath))
  344. {
  345. string qz = Path.GetDirectoryName(LayoutsPath);
  346. if (!string.IsNullOrEmpty(qz) && qz.Contains("Layouts"))
  347. {
  348. string path = ofd.FileName;
  349. string path2 = $"{qz.Replace("Layouts", "Images")}/{ofd.SafeFileName}";
  350. FileInfo fi1 = new FileInfo(path);
  351. FileInfo fi2 = new FileInfo(path2);
  352. try
  353. {
  354. if (!Directory.Exists(qz.Replace("Layouts", "Images"))) // 返回bool类型,存在返回true,不存在返回false
  355. {
  356. Directory.CreateDirectory(qz.Replace("Layouts", "Images")); //不存在则创建路径
  357. }
  358. if (File.Exists(path2))
  359. {
  360. fi2.Delete();
  361. }
  362. fi1.CopyTo(path2);
  363. ((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = System.IO.Path.GetFileName(ofd.FileName);
  364. }
  365. catch (IOException ioex)
  366. {
  367. Console.WriteLine(ioex.Message);
  368. }
  369. }
  370. }
  371. }
  372. }
  373. /// <summary>
  374. /// 选择路径
  375. /// </summary>
  376. /// <param name="obj"></param>
  377. public void SelectValuePath(object obj)
  378. {
  379. OpenFileDialog ofd = new OpenFileDialog();
  380. ofd.Filter = "请选择|*.*";
  381. if (ofd.ShowDialog() == true)
  382. {
  383. ((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = ofd.SafeFileName;
  384. }
  385. }
  386. #endregion
  387. #region 命令事件,页面编辑 Command
  388. /// <summary>
  389. /// 设置首页
  390. /// </summary>
  391. public RelayCommand SetHomePageCommand { get; set; }
  392. /// <summary>
  393. /// 向上移动
  394. /// </summary>
  395. public RelayCommand SetTopPageCommand { get; set; }
  396. /// <summary>
  397. /// 向下移动
  398. /// </summary>
  399. public RelayCommand SetBottomPageCommand { get; set; }
  400. /// <summary>
  401. /// 增加页-该项下增加
  402. /// </summary>
  403. public RelayCommand<object> AddPageCommand { get; set; }
  404. /// <summary>
  405. /// 删除该页
  406. /// </summary>
  407. public RelayCommand<object> DeletePageCommand { get; set; }
  408. /// <summary>
  409. /// 清除当前页子集
  410. /// </summary>
  411. public RelayCommand<object> ClearPageCommand { get; set; }
  412. /// <summary>
  413. /// 选中该页
  414. /// </summary>
  415. public RelayCommand<object> SelectPageCommand { get; set; }
  416. /// <summary>
  417. /// 导出页面文件
  418. /// </summary>
  419. public RelayCommand SaveAllPageCommand { get; set; }
  420. /// <summary>
  421. /// 另存页面文件
  422. /// </summary>
  423. public RelayCommand LWSaveAllPageCommand { get; set; }
  424. /// <summary>
  425. /// 加载页面文件
  426. /// </summary>
  427. public RelayCommand LoadAllPageCommand { get; set; }
  428. /// <summary>
  429. /// 加载变量文件
  430. /// </summary>
  431. public RelayCommand LoadValuesCommand { get; set; }
  432. /// <summary>
  433. /// 初始化命令
  434. /// </summary>
  435. public void InitCommand()
  436. {
  437. SetHomePageCommand = new RelayCommand(new Action(SetHomePageHeader));
  438. SetTopPageCommand = new RelayCommand(new Action(SetTopPageHeader));
  439. SetBottomPageCommand = new RelayCommand(new Action(SetBottomPageHeader));
  440. AddPageCommand = new RelayCommand<object>(new Action<object?>(AddPageHeader));
  441. DeletePageCommand = new RelayCommand<object>(new Action<object?>(DeletePageHeader));
  442. ClearPageCommand = new RelayCommand<object>(new Action<object?>(ClearPageHeader));
  443. SelectPageCommand = new RelayCommand<object>(new Action<object?>(SelectPageHeader));
  444. SaveAllPageCommand = new RelayCommand(new Action(SaveAllPageHeader));
  445. LWSaveAllPageCommand=new RelayCommand(new Action(LWSaveAllPageHeader));
  446. LoadAllPageCommand = new RelayCommand(new Action(LoadAllPageHeader));
  447. LoadValuesCommand = new RelayCommand(new Action(LoadValuesHeader));
  448. MenuModel = new MenuModel();
  449. MenuModel.pageModels = new ObservableCollection<PageModel>();
  450. MenuModel.pageModels.Add(new PageModel { Name = "首页面", Sort = 0 });
  451. MenuModel.SelectPageModels = MenuModel.pageModels[0];
  452. GxSortTarget();
  453. }
  454. /// <summary>
  455. /// 刷新排序
  456. /// </summary>
  457. public void GxSortTarget()
  458. {
  459. for (int i = 0; i < MenuModel.pageModels.Count; i++)
  460. {
  461. MenuModel.pageModels[i].Sort = i;
  462. if(i==0)
  463. MenuModel.pageModels[i].IsHome = true;
  464. else
  465. MenuModel.pageModels[i].IsHome = false;
  466. }
  467. }
  468. /// <summary>
  469. /// 设置首页
  470. /// </summary>
  471. public void SetHomePageHeader()
  472. {
  473. try
  474. {
  475. if (MenuModel.SelectPageModels != null)
  476. {
  477. PageModel mode = MenuModel.SelectPageModels;
  478. MenuModel.pageModels.Remove(mode);
  479. MenuModel.pageModels.Insert(0,mode);
  480. GxSortTarget();
  481. }
  482. }
  483. catch (Exception ex)
  484. {
  485. }
  486. }
  487. /// <summary>
  488. /// 向上移动
  489. /// </summary>
  490. public void SetTopPageHeader()
  491. {
  492. try
  493. {
  494. if (MenuModel.SelectPageModels != null)
  495. {
  496. PageModel mode = MenuModel.SelectPageModels;
  497. if (mode.Sort > 0)
  498. {
  499. MenuModel.pageModels.Remove(mode);
  500. MenuModel.pageModels.Insert(mode.Sort - 1, mode);
  501. GxSortTarget();
  502. }
  503. }
  504. }
  505. catch (Exception ex)
  506. {
  507. }
  508. }
  509. /// <summary>
  510. /// 向下移动
  511. /// </summary>
  512. public void SetBottomPageHeader()
  513. {
  514. try
  515. {
  516. if (MenuModel.SelectPageModels != null)
  517. {
  518. PageModel mode = MenuModel.SelectPageModels;
  519. if (mode.Sort > 0 && mode.Sort+1< MenuModel.pageModels.Count)
  520. {
  521. MenuModel.pageModels.Remove(mode);
  522. MenuModel.pageModels.Insert(mode.Sort + 1, mode);
  523. GxSortTarget();
  524. }
  525. }
  526. }
  527. catch (Exception ex)
  528. {
  529. }
  530. }
  531. /// <summary>
  532. /// 增加页-该项下增加
  533. /// </summary>
  534. public void AddPageHeader(object obj)
  535. {
  536. try
  537. {
  538. if (obj != null && obj is PageModel mode)
  539. {
  540. if (mode.Sort >= 0 && mode.Sort + 1 < MenuModel.pageModels.Count)
  541. {
  542. MenuModel.pageModels.Insert(mode.Sort + 1,new PageModel { Name = "页面"+ MenuModel.pageModels.Count, Sort = MenuModel.pageModels.Count });
  543. GxSortTarget();
  544. }
  545. else if (mode.Sort + 1 == MenuModel.pageModels.Count)
  546. {
  547. MenuModel.pageModels.Add(new PageModel { Name = "页面" + MenuModel.pageModels.Count, Sort = MenuModel.pageModels.Count });
  548. GxSortTarget();
  549. }
  550. }
  551. }
  552. catch (Exception ex)
  553. {
  554. }
  555. }
  556. /// <summary>
  557. /// 删除该页
  558. /// </summary>
  559. public void DeletePageHeader(object obj)
  560. {
  561. try
  562. {
  563. if (obj != null && obj is PageModel page && MenuModel.pageModels.Count > 1)
  564. {
  565. if (new MyMessageBox(MessageBoxStyle.question, $"当前选中页面名称:{page.Name},确认是否删除!").ShowDialog() == true)
  566. {
  567. if (MenuModel.SelectPageModels == page)
  568. {
  569. MenuModel.pageModels.Remove(page);
  570. GxSortTarget();
  571. SelectPageHeader(MenuModel.pageModels[0]);
  572. }
  573. else
  574. {
  575. MenuModel.pageModels.Remove(page);
  576. GxSortTarget();
  577. }
  578. }
  579. }
  580. else if (obj != null)//最后一页
  581. {
  582. if (new MyMessageBox(MessageBoxStyle.question, "至少保留一页,是否清空该页!").ShowDialog() == true)
  583. {
  584. (obj as PageModel).visual.Children.Clear();
  585. (obj as PageModel).ChildrenStr.Clear();
  586. SelectPageHeader(MenuModel.pageModels[0]);
  587. }
  588. }
  589. }
  590. catch (Exception ex)
  591. {
  592. }
  593. }
  594. /// <summary>
  595. /// 清空该页
  596. /// </summary>
  597. /// <param name="obj"></param>
  598. public void ClearPageHeader(object obj)
  599. {
  600. try
  601. {
  602. if (obj != null && obj is PageModel page)
  603. {
  604. if (new MyMessageBox(MessageBoxStyle.question, $"页面名称:{page.Name},是否清空该页!").ShowDialog() == true)
  605. {
  606. (obj as PageModel).visual.Children.Clear();
  607. (obj as PageModel).ChildrenStr.Clear();
  608. if (MenuModel.SelectPageModels == page) SelectPageHeader(obj);
  609. }
  610. }
  611. }
  612. catch (Exception ex)
  613. {
  614. }
  615. }
  616. /// <summary>
  617. /// 选中该页
  618. /// </summary>
  619. /// <param name="obj"></param>
  620. public void SelectPageHeader(object obj)
  621. {
  622. try
  623. {
  624. MenuModel.SelectPageModels = obj as PageModel;
  625. try
  626. {
  627. canvasPanel.Children.Clear();
  628. foreach (FrameworkElement element in (MenuModel.SelectPageModels?.visual as CanvasPanel).Children)
  629. {
  630. string xamlText = XamlWriter.Save(element);
  631. FrameworkElement item = XamlReader.Parse(xamlText) as FrameworkElement;
  632. canvasPanel.Children.Add(item);
  633. }
  634. canvasPanel.SelectedItems?.Clear();
  635. }
  636. catch (Exception ex)
  637. {
  638. }
  639. }
  640. catch (Exception ex)
  641. {
  642. }
  643. }
  644. /// <summary>
  645. /// 保存页面文件
  646. /// </summary>
  647. public void SaveAllPageHeader()
  648. {
  649. try
  650. {
  651. string _path = $"{System.AppDomain.CurrentDomain.BaseDirectory}Layouts\\可视化界面菜单布局.yf";
  652. if (!Directory.Exists($"{System.AppDomain.CurrentDomain.BaseDirectory}Layouts")) // 返回bool类型,存在返回true,不存在返回false
  653. {
  654. Directory.CreateDirectory($"{System.AppDomain.CurrentDomain.BaseDirectory}Layouts"); //不存在则创建路径
  655. }
  656. if (File.Exists(_path)) // 返回bool类型,存在返回true,不存在返回false
  657. {
  658. File.Delete(_path);
  659. }
  660. SaveAllPageHeaderPath(_path);
  661. }
  662. catch (Exception ex)
  663. {
  664. }
  665. }
  666. /// <summary>
  667. /// 另存文件
  668. /// </summary>
  669. public void LWSaveAllPageHeader()
  670. {
  671. try
  672. {
  673. SaveFileDialog sfd = new SaveFileDialog();
  674. sfd.Filter = "页面布局|*.yf";
  675. if (sfd.ShowDialog() == true)
  676. {
  677. SaveAllPageHeaderPath(sfd.FileName);
  678. }
  679. }
  680. catch (Exception ex)
  681. {
  682. }
  683. }
  684. /// <summary>
  685. /// 根据路径保存菜单布局
  686. /// </summary>
  687. /// <param name="path"></param>
  688. public void SaveAllPageHeaderPath(string path)
  689. {
  690. try
  691. {
  692. BinaryFile.SaveBinary(MenuModel, path);
  693. new MyMessageBox(MessageBoxStyle.info, $"保存成功!").ShowDialog();
  694. }
  695. catch (Exception ex)
  696. {
  697. new MyMessageBox(MessageBoxStyle.error, "保存失败!原因:" + ex.Message).ShowDialog();
  698. }
  699. }
  700. /// <summary>
  701. /// 加载页面文件
  702. /// </summary>
  703. public void LoadAllPageHeader()
  704. {
  705. try
  706. {
  707. OpenFileDialog ofd = new OpenFileDialog();
  708. ofd.Filter = "页面布局|*.yf";
  709. if (ofd.ShowDialog() == true)
  710. {
  711. LoadAllPageHeaderPath(ofd.FileName);
  712. }
  713. }
  714. catch (Exception ex)
  715. {
  716. }
  717. }
  718. /// <summary>
  719. /// 根据路径加载菜单布局
  720. /// </summary>
  721. /// <param name="path"></param>
  722. public void LoadAllPageHeaderPath(string path)
  723. {
  724. try
  725. {
  726. MenuModel = BinaryFile.ReadBinary<MenuModel>(path) as MenuModel;
  727. MenuModel?.pageModels?.ToList().ForEach(par =>
  728. {
  729. par.visual = new CanvasPanel();
  730. par?.ChildrenStr?.ForEach(k =>
  731. {
  732. FrameworkElement item = XamlReader.Parse(k) as FrameworkElement;
  733. par.visual.Children.Add(item);
  734. });
  735. });
  736. if (MenuModel.pageModels != null && MenuModel.pageModels.Count > 0)
  737. {
  738. SelectPageHeader(MenuModel.pageModels?.ToList()?[0]);
  739. }
  740. }
  741. catch (Exception ex)
  742. {
  743. }
  744. }
  745. /// <summary>
  746. /// 加载变量文件
  747. /// </summary>
  748. public void LoadValuesHeader()
  749. {
  750. try
  751. {
  752. string path= AppDomain.CurrentDomain.BaseDirectory + "BPASmart.VariableManager.exe";
  753. if (File.Exists(path))
  754. {
  755. Process.Start(path);
  756. }
  757. }
  758. catch (Exception ex)
  759. {
  760. }
  761. //OpenFileDialog ofd = new OpenFileDialog();
  762. //ofd.Filter = "变量管理器文件|*.json";
  763. //if (ofd.ShowDialog() == true)
  764. //{
  765. // VariablePath = ofd.FileName;
  766. // bool isSucess = DataBusModel.GetInstance().RefreshVariableManager(ofd.FileName);
  767. // System.Windows.MessageBox.Show(isSucess ? "加载成功!" : "加载失败!");
  768. //}
  769. }
  770. #endregion
  771. }
  772. }