From 9c661200b981efaf5dae4be3daee05fa66859714 Mon Sep 17 00:00:00 2001 From: hsc Date: Thu, 11 Jun 2026 10:42:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BE=9D=E8=B5=96=E6=B3=A8=E5=85=A5=E9=A1=BA?= =?UTF-8?q?=E5=BA=8F=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ViewModels/AutomatedTestingViewModel.cs | 31 +++++++++---------- .../ViewModels/ProtocolStartViewModel.cs | 15 ++++----- UIShare/GlobalVariable/ConfigService.cs | 4 +-- UIShare/GlobalVariable/DeviceManager.cs | 4 +-- UIShare/GlobalVariable/GlobalInfo.cs | 1 + UIShare/GlobalVariable/StepRunning.cs | 4 ++- 6 files changed, 27 insertions(+), 32 deletions(-) diff --git a/MainModule/ViewModels/AutomatedTestingViewModel.cs b/MainModule/ViewModels/AutomatedTestingViewModel.cs index d9f4a7d..aa12dd7 100644 --- a/MainModule/ViewModels/AutomatedTestingViewModel.cs +++ b/MainModule/ViewModels/AutomatedTestingViewModel.cs @@ -52,11 +52,22 @@ namespace MainModule.ViewModels // 每个 AutomatedTestingViewModel 实例创建独立的容器作用域 _scope = container.CreateScope(); _globalInfo =container.Resolve(); - // 在该作用域内解析 ScopedContext —— 当前作用域唯一 - _scopedContext = _scope.Resolve(); - _stepRunning = _scope.Resolve(); _systemConfig = _scope.Resolve(); - // 关键:从同一个 _scope 解析 5 个子 VM,DI 会把同一个 ScopedContext 注入它们 + //加载Json数据 + if (ConfigService.IsExit(_globalInfo.CurrentOpeningScope)) + { + string filePath = System.IO.Path.Combine(_systemConfig.SystemPath, $"{_globalInfo.CurrentOpeningScope}.json"); + if (System.IO.File.Exists(filePath)) + { + string json = System.IO.File.ReadAllText(filePath); + Newtonsoft.Json.JsonConvert.PopulateObject(json, _systemConfig); + } + } + //容器解析顺序不要改变!!! + _scopedContext = _scope.Resolve(); + _deviceManager = _scope.Resolve(); + _stepRunning = _scope.Resolve(); + // 从同一个 _scope 解析 5 个子 VM,DI 会把同一个 ScopedContext 注入它们 CommandTreeVM = _scope.Resolve(); StepsManagerVM = _scope.Resolve(); SingleStepEditVM = _scope.Resolve(); @@ -111,17 +122,6 @@ namespace MainModule.ViewModels if (navigationContext.Parameters.ContainsKey("Name")) { TestStatus = navigationContext.Parameters.GetValue("Name"); - if (ConfigService.IsExit(TestStatus)) - { - string filePath = System.IO.Path.Combine(_systemConfig.SystemPath, $"{TestStatus}.json"); - if (System.IO.File.Exists(filePath)) - { - string json = System.IO.File.ReadAllText(filePath); - - // 🔥 关键:把 json 数据直接灌入当前实例,对象引用没有任何改变 - Newtonsoft.Json.JsonConvert.PopulateObject(json, _systemConfig); - } - } _globalInfo.ContextDic.Add(TestStatus, _scopedContext); _globalInfo.StepRunningDic.Add(TestStatus, _stepRunning); _globalInfo.ScopeDic.Add(TestStatus, _scope); @@ -146,7 +146,6 @@ namespace MainModule.ViewModels _scopedContext.Program.StepCollection = program.StepCollection; _scopedContext.Program.ErrorStepCollection = program.ErrorStepCollection; _scopedContext.CurrentFilePath = filePath; - _deviceManager = _scope.Resolve(); } } diff --git a/MainModule/ViewModels/ProtocolStartViewModel.cs b/MainModule/ViewModels/ProtocolStartViewModel.cs index d0c21ce..259505b 100644 --- a/MainModule/ViewModels/ProtocolStartViewModel.cs +++ b/MainModule/ViewModels/ProtocolStartViewModel.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; +using UIShare.GlobalVariable; using UIShare.ViewModelBase; namespace MainModule.ViewModels @@ -15,6 +16,7 @@ namespace MainModule.ViewModels #region 私有字段 private string _testStatus; private string _moduleColor; + private GlobalInfo _globalInfo; #endregion #region 属性 @@ -24,11 +26,7 @@ namespace MainModule.ViewModels set => SetProperty(ref _testStatus, value); } - public string ModuleColor - { - get => _moduleColor; - set => SetProperty(ref _moduleColor, value); - } + #endregion #region 命令 @@ -37,6 +35,7 @@ namespace MainModule.ViewModels public ProtocolStartViewModel(IContainerProvider containerProvider) : base(containerProvider) { + _globalInfo = containerProvider.Resolve(); StartProtocolCommand = new DelegateCommand(OnStart); } @@ -69,8 +68,7 @@ namespace MainModule.ViewModels // 2. 透传名称与颜色参数,使 AutomatedTestingViewModel 能正确初始化 var parameters = new NavigationParameters(); parameters.Add("Name", TestStatus); - parameters.Add("Color", ModuleColor); - + _globalInfo.CurrentOpeningScope = TestStatus; // 3. 在本格子 Region 内请求导航,导航成功后再移除旧的 ProtocolStartView 以释放资源 _regionManager.RequestNavigate(regionName, viewName, navResult => { @@ -102,8 +100,7 @@ namespace MainModule.ViewModels if (navigationContext.Parameters.ContainsKey("Name")) TestStatus = navigationContext.Parameters.GetValue("Name"); - if (navigationContext.Parameters.ContainsKey("Color")) - ModuleColor = navigationContext.Parameters.GetValue("Color"); + } #endregion } diff --git a/UIShare/GlobalVariable/ConfigService.cs b/UIShare/GlobalVariable/ConfigService.cs index f54eea6..f556206 100644 --- a/UIShare/GlobalVariable/ConfigService.cs +++ b/UIShare/GlobalVariable/ConfigService.cs @@ -18,9 +18,7 @@ namespace UIShare.GlobalVariable return false; } - // 临时实例化一个对象以获取默认的 SystemPath - var dummy = new SystemConfig(); - string configPath = Path.Combine(dummy.SystemPath, $"{title}.json"); + string configPath = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ADP"), $"{title}.json"); if (!File.Exists(configPath)) { diff --git a/UIShare/GlobalVariable/DeviceManager.cs b/UIShare/GlobalVariable/DeviceManager.cs index a2dc989..6b871d5 100644 --- a/UIShare/GlobalVariable/DeviceManager.cs +++ b/UIShare/GlobalVariable/DeviceManager.cs @@ -17,7 +17,6 @@ namespace UIShare.GlobalVariable public class DeviceManager { public SystemConfig _systemConfig { get; set; } - public IList DeviceList { get; private set; } = new List(); /// 按 DeviceName 索引的设备字典,便于业务层按名取实例。 public IDictionary DeviceMap { get; private set; } @@ -30,6 +29,7 @@ namespace UIShare.GlobalVariable { _systemConfig = systemConfig; InitDevices(); + } /// @@ -83,8 +83,6 @@ namespace UIShare.GlobalVariable LoggerHelper.Warn($"设备 [{config.DeviceName}] 连接方式 [{config.ConnectionType}] 不支持,已跳过。"); continue; } - - DeviceList.Add(instance); if (!string.IsNullOrWhiteSpace(config.DeviceName)) { DeviceMap[config.DeviceName] = instance; diff --git a/UIShare/GlobalVariable/GlobalInfo.cs b/UIShare/GlobalVariable/GlobalInfo.cs index 051798f..ab5fc5c 100644 --- a/UIShare/GlobalVariable/GlobalInfo.cs +++ b/UIShare/GlobalVariable/GlobalInfo.cs @@ -15,6 +15,7 @@ namespace UIShare.GlobalVariable public Dictionary ScopeDic { get; set; } public String UserName { get; set; } = "Not Logged in"; public bool IsAdmin { get; set; } = true; + public string CurrentOpeningScope; private string _currentScope = "default"; public string CurrentScope { diff --git a/UIShare/GlobalVariable/StepRunning.cs b/UIShare/GlobalVariable/StepRunning.cs index eed91ba..e426eff 100644 --- a/UIShare/GlobalVariable/StepRunning.cs +++ b/UIShare/GlobalVariable/StepRunning.cs @@ -22,6 +22,7 @@ namespace UIShare { private ScopedContext _scopedContext; private SystemConfig _systemConfig; + private DeviceManager _deviceManager; //private Devices _devices; private IContainerProvider containerProvider; private IEventAggregator _eventAggregator; @@ -39,11 +40,12 @@ namespace UIShare private bool SubSingleStep = false; private Guid TestRoundID; - public StepRunning(ScopedContext ScopedContext, SystemConfig systemConfig,IEventAggregator eventAggregator,IContainerProvider containerProvider) + public StepRunning(ScopedContext ScopedContext, SystemConfig systemConfig,IEventAggregator eventAggregator, DeviceManager deviceManager) { _scopedContext = ScopedContext; _systemConfig = systemConfig; _eventAggregator = eventAggregator; + _deviceManager= deviceManager; //_devices = containerProvider.Resolve(); } public async Task ExecuteErrorSteps(ProgramVM program, int depth = 0, CancellationToken cancellationToken = default)