using ICSharpCode.AvalonEdit.Highlighting; using ICSharpCode.AvalonEdit; using Prism.Commands; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Linq; using Microsoft.Win32; using ICSharpCode.AvalonEdit.CodeCompletion; using TeamAAS_VP.Core; using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox; using System.Windows.Documents; using ICSharpCode.AvalonEdit.Document; using TeamAAS_VP.Models; using System.Collections.ObjectModel; using System.Windows.Media; using System.Text.RegularExpressions; using System.Windows.Controls; using ICSharpCode.AvalonEdit.Editing; using System.Reflection; using System.Threading.Tasks; using System.ComponentModel; using System.Windows.Input; using System.IO; using TeamAAS_VP; using System.Windows; using Cognex.VisionPro.ToolBlock; using Cognex.VisionPro; using MaterialDesignThemes.Wpf; using TeamAAS_VP.Controls; using TeamAAS_VP.Events; using Prism.Events; using Prism.Ioc; using Prism.Regions; using TouchSocket.Core; using TeamAAS_VP.Resources.Languages; namespace TeamAAS_VP.ViewModels.DebugMod { public class ScriptHomeViewModel : BindableBase { IRegionManager _regionManager; IEventAggregator _eventAggregator; IContainerProvider _container; string _oldScript = ""; string _oldFile = ""; #region 属性 private string _Scripting; public string Scripting { get { return _Scripting; } set { SetProperty(ref _Scripting, value); } } private string _ScriptingFilePath; public string ScriptingFilePath { get { return _ScriptingFilePath; } set { SetProperty(ref _ScriptingFilePath, value); } } private ObservableCollection _ScriptFilesCollection; public ObservableCollection ScriptFilesCollection { get { return _ScriptFilesCollection; } set { SetProperty(ref _ScriptFilesCollection, value); } } private string _SelectScript; public string SelectScript { get { return _SelectScript; } set { SetProperty(ref _SelectScript, value); if (!string.IsNullOrEmpty(value)) { IsSelected = true; } else { IsSelected = false; } } } private bool _IsSelected=false; public bool IsSelected { get { return _IsSelected; } set { SetProperty(ref _IsSelected, value); } } #endregion #region 命令 private DelegateCommand _LoadedCommand; public DelegateCommand LoadedCommand => _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand)); private DelegateCommand _SaveScriptCommand; public DelegateCommand SaveScriptCommand => _SaveScriptCommand ?? (_SaveScriptCommand = new DelegateCommand(ExecuteSaveScriptCommand)); private DelegateCommand _SelectScriptCommand; public DelegateCommand SelectScriptCommand => _SelectScriptCommand ?? (_SelectScriptCommand = new DelegateCommand(ExecuteSelectScriptCommand)); private DelegateCommand _CreateFileCommand; public DelegateCommand CreateFileCommand => _CreateFileCommand ?? (_CreateFileCommand = new DelegateCommand(ExecuteCreateFileCommand)); private DelegateCommand _DelectFileCommand; public DelegateCommand DelectFileCommand => _DelectFileCommand ?? (_DelectFileCommand = new DelegateCommand(ExecuteDelectFileCommand).ObservesCanExecute(()=> IsSelected)); #endregion #region 事件 #endregion public ScriptHomeViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container) { _regionManager = regionManager; _eventAggregator = ea; _container = container; } #region 方法 bool isFirst = true; void ExecuteLoadedCommand() { if (isFirst) { if (!Directory.Exists(FilePath.BackgroundScriptPath)) { Directory.CreateDirectory(FilePath.BackgroundScriptPath); } try { string[] files = Directory.GetFiles(FilePath.BackgroundScriptPath); ScriptFilesCollection=new ObservableCollection(); foreach (string file in files) { string fileName = Path.GetFileName(file); string fileExtension = Path.GetExtension(file); if (fileExtension==".cs") { ScriptFilesCollection.Add(fileName); } } } catch (Exception ex) { LogHelper.WriteLogError(Lang.读取本地脚本文件列表时出错,ex); } isFirst = false; } } /// /// 创建脚本 /// async void ExecuteCreateFileCommand() { try { var view = new AddProduct() { Title = $"{Lang.创建脚本文件}:", Item = "Name" }; //show the dialog var result = await DialogHost.Show(view, "RootDialog", null, null, null); if (result != null && !string.IsNullOrEmpty(result.ToString())) { if (!FileHelper.CheckFileName(result.ToString())) { _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = Lang.输入的名称不符合文件名命名规则, Duration = 1 }); return; } //检查是否已有同名文件 var file = ScriptFilesCollection.FirstOrDefault(c => c == result.ToString() + ".cs"); if (file != null) { _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = Lang.已存在同名脚本文件, Duration = 1 }); return; } if (File.Exists(FilePath.ScriptBackgroundTemplatePath)) { File.Copy(FilePath.ScriptBackgroundTemplatePath, $"{FilePath.BackgroundScriptPath}//{result.ToString()}.cs"); await Task.Delay(200); } else { File.Create($"{FilePath.BackgroundScriptPath}//{result.ToString()}.cs"); await Task.Delay(200); } ScriptFilesCollection.Add(result.ToString() + ".cs"); SelectScript = result.ToString() + ".cs"; Scripting = FileHelper.ReadFile($"{FilePath.BackgroundScriptPath}//{result.ToString()}.cs"); //ExecuteSelectScriptCommand(SelectScript); } } catch (Exception ex) { LogHelper.WriteLogError(Lang.创建脚本时出错, ex); MessageBox.Show(ex.Message); } } /// /// 移除文件 /// /// void ExecuteDelectFileCommand(string filename) { try { if (MessageBox.Show(Lang.是否移除脚本文件.Replace("{0}", filename),"",MessageBoxButton.OKCancel,MessageBoxImage.Question)== MessageBoxResult.OK) { var file = ScriptFilesCollection.FirstOrDefault(c => c == filename); if (file != null) { Scripting = ""; _oldScript = Scripting; ScriptFilesCollection.Remove(file); SelectScript = null; File.Delete($"{FilePath.BackgroundScriptPath}//{file}"); } } } catch (Exception ex) { LogHelper.WriteLogError(Lang.移除文件时出错, ex); MessageBox.Show(ex.Message); } } /// /// 选中脚本文件时出错 /// /// void ExecuteSelectScriptCommand(string filename) { try { if (!string.IsNullOrEmpty(_oldFile) && _oldFile != "" && !string.Equals(_oldScript, Scripting)) { if (MessageBox.Show(Lang.是否保存脚本, Lang.保存脚本, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { FileHelper.WriteFile(Scripting, $"{FilePath.BackgroundScriptPath}//{_oldFile}"); } } else { _oldFile = filename; } if (!string.IsNullOrEmpty(filename)) { ScriptingFilePath = $"{FilePath.BackgroundScriptPath}//{_oldFile}"; Scripting = FileHelper.ReadFile(ScriptingFilePath); _oldScript = Scripting; } else { ScriptingFilePath = string.Empty; Scripting = string.Empty; _oldScript = Scripting; } } catch (Exception ex) { LogHelper.WriteLogError(Lang.选中脚本文件时出错, ex); } } void ExecuteSaveScriptCommand() { _oldScript = Scripting; } #endregion } }