ScriptHomeViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using ICSharpCode.AvalonEdit.Highlighting;
  2. using ICSharpCode.AvalonEdit;
  3. using Prism.Commands;
  4. using Prism.Mvvm;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using Microsoft.Win32;
  9. using ICSharpCode.AvalonEdit.CodeCompletion;
  10. using TeamAAS_VP.Core;
  11. using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;
  12. using System.Windows.Documents;
  13. using ICSharpCode.AvalonEdit.Document;
  14. using TeamAAS_VP.Models;
  15. using System.Collections.ObjectModel;
  16. using System.Windows.Media;
  17. using System.Text.RegularExpressions;
  18. using System.Windows.Controls;
  19. using ICSharpCode.AvalonEdit.Editing;
  20. using System.Reflection;
  21. using System.Threading.Tasks;
  22. using System.ComponentModel;
  23. using System.Windows.Input;
  24. using System.IO;
  25. using TeamAAS_VP;
  26. using System.Windows;
  27. using Cognex.VisionPro.ToolBlock;
  28. using Cognex.VisionPro;
  29. using MaterialDesignThemes.Wpf;
  30. using TeamAAS_VP.Controls;
  31. using TeamAAS_VP.Events;
  32. using Prism.Events;
  33. using Prism.Ioc;
  34. using Prism.Regions;
  35. using TouchSocket.Core;
  36. using TeamAAS_VP.Resources.Languages;
  37. namespace TeamAAS_VP.ViewModels.DebugMod
  38. {
  39. public class ScriptHomeViewModel : BindableBase
  40. {
  41. IRegionManager _regionManager;
  42. IEventAggregator _eventAggregator;
  43. IContainerProvider _container;
  44. string _oldScript = "";
  45. string _oldFile = "";
  46. #region 属性
  47. private string _Scripting;
  48. public string Scripting
  49. {
  50. get { return _Scripting; }
  51. set { SetProperty(ref _Scripting, value); }
  52. }
  53. private string _ScriptingFilePath;
  54. public string ScriptingFilePath
  55. {
  56. get { return _ScriptingFilePath; }
  57. set { SetProperty(ref _ScriptingFilePath, value); }
  58. }
  59. private ObservableCollection<string> _ScriptFilesCollection;
  60. public ObservableCollection<string> ScriptFilesCollection
  61. {
  62. get { return _ScriptFilesCollection; }
  63. set { SetProperty(ref _ScriptFilesCollection, value); }
  64. }
  65. private string _SelectScript;
  66. public string SelectScript
  67. {
  68. get { return _SelectScript; }
  69. set { SetProperty(ref _SelectScript, value);
  70. if (!string.IsNullOrEmpty(value))
  71. {
  72. IsSelected = true;
  73. }
  74. else
  75. {
  76. IsSelected = false;
  77. }
  78. }
  79. }
  80. private bool _IsSelected=false;
  81. public bool IsSelected
  82. {
  83. get { return _IsSelected; }
  84. set { SetProperty(ref _IsSelected, value); }
  85. }
  86. #endregion
  87. #region 命令
  88. private DelegateCommand _LoadedCommand;
  89. public DelegateCommand LoadedCommand =>
  90. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  91. private DelegateCommand _SaveScriptCommand;
  92. public DelegateCommand SaveScriptCommand =>
  93. _SaveScriptCommand ?? (_SaveScriptCommand = new DelegateCommand(ExecuteSaveScriptCommand));
  94. private DelegateCommand<string> _SelectScriptCommand;
  95. public DelegateCommand<string> SelectScriptCommand =>
  96. _SelectScriptCommand ?? (_SelectScriptCommand = new DelegateCommand<string>(ExecuteSelectScriptCommand));
  97. private DelegateCommand _CreateFileCommand;
  98. public DelegateCommand CreateFileCommand =>
  99. _CreateFileCommand ?? (_CreateFileCommand = new DelegateCommand(ExecuteCreateFileCommand));
  100. private DelegateCommand<string> _DelectFileCommand;
  101. public DelegateCommand<string> DelectFileCommand =>
  102. _DelectFileCommand ?? (_DelectFileCommand = new DelegateCommand<string>(ExecuteDelectFileCommand).ObservesCanExecute(()=> IsSelected));
  103. #endregion
  104. #region 事件
  105. #endregion
  106. public ScriptHomeViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container)
  107. {
  108. _regionManager = regionManager;
  109. _eventAggregator = ea;
  110. _container = container;
  111. }
  112. #region 方法
  113. bool isFirst = true;
  114. void ExecuteLoadedCommand()
  115. {
  116. if (isFirst)
  117. {
  118. if (!Directory.Exists(FilePath.BackgroundScriptPath))
  119. {
  120. Directory.CreateDirectory(FilePath.BackgroundScriptPath);
  121. }
  122. try
  123. {
  124. string[] files = Directory.GetFiles(FilePath.BackgroundScriptPath);
  125. ScriptFilesCollection=new ObservableCollection<string>();
  126. foreach (string file in files)
  127. {
  128. string fileName = Path.GetFileName(file);
  129. string fileExtension = Path.GetExtension(file);
  130. if (fileExtension==".cs")
  131. {
  132. ScriptFilesCollection.Add(fileName);
  133. }
  134. }
  135. }
  136. catch (Exception ex)
  137. {
  138. LogHelper.WriteLogError("读取本地脚本文件列表时出错",ex);
  139. }
  140. isFirst = false;
  141. }
  142. }
  143. /// <summary>
  144. /// 创建脚本
  145. /// </summary>
  146. async void ExecuteCreateFileCommand()
  147. {
  148. try
  149. {
  150. var view = new AddProduct() { Title = $"{Lang.创建脚本文件}:", Item = "Name" };
  151. //show the dialog
  152. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  153. if (result != null && !string.IsNullOrEmpty(result.ToString()))
  154. {
  155. if (!FileHelper.CheckFileName(result.ToString()))
  156. {
  157. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.输入的名称不符合文件名命名规则, Duration = 1 });
  158. return;
  159. }
  160. //检查是否已有同名文件
  161. var file = ScriptFilesCollection.FirstOrDefault(c => c == result.ToString() + ".cs");
  162. if (file != null)
  163. {
  164. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.已存在同名脚本文件, Duration = 1 });
  165. return;
  166. }
  167. if (File.Exists(FilePath.ScriptBackgroundTemplatePath))
  168. {
  169. File.Copy(FilePath.ScriptBackgroundTemplatePath, $"{FilePath.BackgroundScriptPath}//{result.ToString()}.cs");
  170. await Task.Delay(200);
  171. }
  172. else
  173. {
  174. File.Create($"{FilePath.BackgroundScriptPath}//{result.ToString()}.cs");
  175. await Task.Delay(200);
  176. }
  177. ScriptFilesCollection.Add(result.ToString() + ".cs");
  178. SelectScript = result.ToString() + ".cs";
  179. Scripting = FileHelper.ReadFile($"{FilePath.BackgroundScriptPath}//{result.ToString()}.cs");
  180. //ExecuteSelectScriptCommand(SelectScript);
  181. }
  182. }
  183. catch (Exception ex)
  184. {
  185. LogHelper.WriteLogError("创建脚本时出错", ex);
  186. MessageBox.Show(ex.Message);
  187. }
  188. }
  189. /// <summary>
  190. /// 移除文件
  191. /// </summary>
  192. /// <param name="filename"></param>
  193. void ExecuteDelectFileCommand(string filename)
  194. {
  195. try
  196. {
  197. if (MessageBox.Show(Lang.是否移除脚本文件.Replace("{0}", filename),"",MessageBoxButton.OKCancel,MessageBoxImage.Question)== MessageBoxResult.OK)
  198. {
  199. var file = ScriptFilesCollection.FirstOrDefault(c => c == filename);
  200. if (file != null)
  201. {
  202. Scripting = "";
  203. _oldScript = Scripting;
  204. ScriptFilesCollection.Remove(file);
  205. SelectScript = null;
  206. File.Delete($"{FilePath.BackgroundScriptPath}//{file}");
  207. }
  208. }
  209. }
  210. catch (Exception ex)
  211. {
  212. LogHelper.WriteLogError("移除文件时出错", ex);
  213. MessageBox.Show(ex.Message);
  214. }
  215. }
  216. /// <summary>
  217. /// 选中脚本文件时出错
  218. /// </summary>
  219. /// <param name="filename"></param>
  220. void ExecuteSelectScriptCommand(string filename)
  221. {
  222. try
  223. {
  224. if (!string.IsNullOrEmpty(_oldFile) && _oldFile != "" && !string.Equals(_oldScript, Scripting))
  225. {
  226. if (MessageBox.Show(Lang.是否保存脚本, Lang.保存脚本, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  227. {
  228. FileHelper.WriteFile(Scripting, $"{FilePath.BackgroundScriptPath}//{_oldFile}");
  229. }
  230. }
  231. else
  232. {
  233. _oldFile = filename;
  234. }
  235. if (!string.IsNullOrEmpty(filename))
  236. {
  237. ScriptingFilePath = $"{FilePath.BackgroundScriptPath}//{_oldFile}";
  238. Scripting = FileHelper.ReadFile(ScriptingFilePath);
  239. _oldScript = Scripting;
  240. }
  241. else
  242. {
  243. ScriptingFilePath = string.Empty;
  244. Scripting = string.Empty;
  245. _oldScript = Scripting;
  246. }
  247. }
  248. catch (Exception ex)
  249. {
  250. LogHelper.WriteLogError("选中脚本文件时出错!", ex);
  251. }
  252. }
  253. void ExecuteSaveScriptCommand()
  254. {
  255. _oldScript = Scripting;
  256. }
  257. #endregion
  258. }
  259. }