| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- 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<string> _ScriptFilesCollection;
- public ObservableCollection<string> 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<string> _SelectScriptCommand;
- public DelegateCommand<string> SelectScriptCommand =>
- _SelectScriptCommand ?? (_SelectScriptCommand = new DelegateCommand<string>(ExecuteSelectScriptCommand));
- private DelegateCommand _CreateFileCommand;
- public DelegateCommand CreateFileCommand =>
- _CreateFileCommand ?? (_CreateFileCommand = new DelegateCommand(ExecuteCreateFileCommand));
- private DelegateCommand<string> _DelectFileCommand;
- public DelegateCommand<string> DelectFileCommand =>
- _DelectFileCommand ?? (_DelectFileCommand = new DelegateCommand<string>(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<string>();
- 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;
- }
-
- }
- /// <summary>
- /// 创建脚本
- /// </summary>
- 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<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.输入的名称不符合文件名命名规则, Duration = 1 });
- return;
- }
- //检查是否已有同名文件
- var file = ScriptFilesCollection.FirstOrDefault(c => c == result.ToString() + ".cs");
- if (file != null)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().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);
- }
- }
- /// <summary>
- /// 移除文件
- /// </summary>
- /// <param name="filename"></param>
- 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);
- }
- }
- /// <summary>
- /// 选中脚本文件时出错
- /// </summary>
- /// <param name="filename"></param>
- 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
- }
- }
|