diff --git a/ADP/ViewModels/ShellViewModel.cs b/ADP/ViewModels/ShellViewModel.cs index 05ffb65..a17894f 100644 --- a/ADP/ViewModels/ShellViewModel.cs +++ b/ADP/ViewModels/ShellViewModel.cs @@ -678,8 +678,9 @@ namespace ADP.ViewModels _globalInfo.ContextDic.Remove(targetRegionName); _globalInfo.StepRunningDic.Remove(targetRegionName); + _globalInfo.ScopeDic.Remove(targetRegionName); - var parameters = new NavigationParameters { { "Name", targetRegionName } }; + var parameters = new NavigationParameters { { "Name", targetRegionName } }; _regionManager.RequestNavigate(targetRegionName, "ProtocolStartView", parameters); _eventAggregator.GetEvent().Publish(""); } diff --git a/MainModule/ViewModels/AutomatedTestingViewModel.cs b/MainModule/ViewModels/AutomatedTestingViewModel.cs index 043837e..28878a5 100644 --- a/MainModule/ViewModels/AutomatedTestingViewModel.cs +++ b/MainModule/ViewModels/AutomatedTestingViewModel.cs @@ -29,7 +29,7 @@ namespace MainModule.ViewModels public ScopedContext _scopedContext { get; } public StepRunning _stepRunning { get; } public GlobalInfo _globalInfo { get; } - public SystemConfig _systemConfig { get; } + public SystemConfig _systemConfig { get; set; } // 5 个子 ViewModel 全部从同一个 scope 解析,自动注入同一个 ScopedContext public CommandTreeViewModel CommandTreeVM { get; } @@ -50,7 +50,6 @@ namespace MainModule.ViewModels // 在该作用域内解析 ScopedContext —— 当前作用域唯一 _scopedContext = _scope.Resolve(); _stepRunning = _scope.Resolve(); - _systemConfig=_scope.Resolve(); // 关键:从同一个 _scope 解析 5 个子 VM,DI 会把同一个 ScopedContext 注入它们 CommandTreeVM = _scope.Resolve(); StepsManagerVM = _scope.Resolve(); @@ -108,6 +107,18 @@ namespace MainModule.ViewModels _globalInfo.ContextDic.Add(TestStatus, _scopedContext); _globalInfo.StepRunningDic.Add(TestStatus, _stepRunning); _globalInfo.ScopeDic.Add(TestStatus, _scope); + _systemConfig = _scope.Resolve(); + 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); + } + } } } diff --git a/MainModule/ViewModels/ProtocolStartViewModel.cs b/MainModule/ViewModels/ProtocolStartViewModel.cs index 2c12011..d0c21ce 100644 --- a/MainModule/ViewModels/ProtocolStartViewModel.cs +++ b/MainModule/ViewModels/ProtocolStartViewModel.cs @@ -79,7 +79,8 @@ namespace MainModule.ViewModels region.Remove(myView); } }, parameters); - + _moduleManager.LoadModule("MonitorModule"); + _moduleManager.LoadModule("SettingModule"); return; } } diff --git a/MonitorModule/MonitorModule.cs b/MonitorModule/MonitorModule.cs index 5099bf7..b1a322c 100644 --- a/MonitorModule/MonitorModule.cs +++ b/MonitorModule/MonitorModule.cs @@ -4,6 +4,7 @@ using System.Reflection; namespace MonitorModule { + [Module(OnDemand = true)] public class MonitorModule: IModule { public void OnInitialized(IContainerProvider containerProvider) diff --git a/MonitorModule/ViewModels/MonitorViewModel.cs b/MonitorModule/ViewModels/MonitorViewModel.cs index b957059..9966fc4 100644 --- a/MonitorModule/ViewModels/MonitorViewModel.cs +++ b/MonitorModule/ViewModels/MonitorViewModel.cs @@ -301,7 +301,7 @@ namespace MonitorModule.ViewModels Plot.Title = $"监控 - {TestStatus}"; Plot.InvalidatePlot(false); _scope = _globalInfo.ScopeDic[TestStatus]; - _scopedContext = _globalInfo.ContextDic[TestStatus]; + _scopedContext = _scope.Resolve(); IsInitiated = true; } } diff --git a/MonitorModule/ViewModels/RecordViewModel.cs b/MonitorModule/ViewModels/RecordViewModel.cs index cd2c86d..b9b27d4 100644 --- a/MonitorModule/ViewModels/RecordViewModel.cs +++ b/MonitorModule/ViewModels/RecordViewModel.cs @@ -345,7 +345,7 @@ namespace MonitorModule.ViewModels { TestStatus = navigationContext.Parameters.GetValue("Name"); _scope = _globalInfo.ScopeDic[TestStatus]; - _scopedContext = _globalInfo.ContextDic[TestStatus]; + _scopedContext = _scope.Resolve(); IsInitiated = true; } LoadTables(); diff --git a/SettingModule/SettingModule.cs b/SettingModule/SettingModule.cs index 32ec4e9..e4c3db0 100644 --- a/SettingModule/SettingModule.cs +++ b/SettingModule/SettingModule.cs @@ -4,6 +4,7 @@ using System.Reflection; namespace SettingModule { + [Module(OnDemand = true)] public class SettingModule : IModule { public void OnInitialized(IContainerProvider containerProvider) diff --git a/SettingModule/ViewModels/SettingViewModel.cs b/SettingModule/ViewModels/SettingViewModel.cs index d370cda..20171fa 100644 --- a/SettingModule/ViewModels/SettingViewModel.cs +++ b/SettingModule/ViewModels/SettingViewModel.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.Windows.Input; using Prism.Commands; @@ -48,7 +49,7 @@ namespace SettingModule.ViewModels public ObservableCollection ConnectionTypes { get; } = new() { - "None", "TCP", "Serial" + "None", "Tcp", "Serial" }; #endregion @@ -72,7 +73,6 @@ namespace SettingModule.ViewModels public SettingViewModel(IContainerExtension container) : base(container) { _globalInfo = container.Resolve(); - RefreshCommand = new DelegateCommand(OnExpand); SaveCommand = new DelegateCommand(OnSave); ResetCommand = new DelegateCommand(OnReset); @@ -107,15 +107,23 @@ namespace SettingModule.ViewModels _eventAggregator.GetEvent().Publish(TestStatus); } - /// 保存当前选中设备的配置(占位,实际写盘逻辑后续接入)。 + /// 保存当前 SystemConfig 到 SystemPath 下,文件名为 {Title}.json。 private void OnSave() { - if (SelectedDevice == null) + if (_systemConfig == null) { - StatusMessage = "无可保存的设备"; + StatusMessage = "无可保存的配置"; return; } - StatusMessage = $"已保存设备 [{SelectedDevice.DeviceName}] 的配置({DateTime.Now:HH:mm:ss})"; + + if (string.IsNullOrWhiteSpace(_systemConfig.Title)) + { + StatusMessage = "保存失败:标题(Title)不能为空"; + return; + } + + ConfigService.Save(_systemConfig); + StatusMessage = $"已保存配置 [{_systemConfig.Title}.json] 至 {_systemConfig.SystemPath}({DateTime.Now:HH:mm:ss})"; } /// 重置当前选中设备的配置(占位)。 @@ -142,7 +150,7 @@ namespace SettingModule.ViewModels var dialogName = SelectedDevice.ConnectionType switch { - "TCP" => "TCPConfig", + "Tcp" => "TCPConfig", "Serial" => "SerialPortConfig", _ => string.Empty }; @@ -153,7 +161,6 @@ namespace SettingModule.ViewModels return; } - // 【关键修改】在此处将选中的设备以及隔离作用域内的 _systemConfig 一并传进弹窗 var p = new DialogParameters { { "Device", SelectedDevice }, @@ -180,12 +187,11 @@ namespace SettingModule.ViewModels _scope = _globalInfo.ScopeDic[TestStatus]; _scopedContext = _globalInfo.ContextDic[TestStatus]; _systemConfig = _scope.Resolve(); - DeviceList = _systemConfig.DeviceList; if (DeviceList != null && DeviceList.Count > 0) { SelectedDevice = DeviceList[0]; } - + DeviceList = _systemConfig.DeviceList; IsInitiated = true; } } diff --git a/SettingModule/Views/Dialogs/SerialPortConfigView.xaml b/SettingModule/Views/Dialogs/SerialPortConfigView.xaml index 56effb7..64fb8d3 100644 --- a/SettingModule/Views/Dialogs/SerialPortConfigView.xaml +++ b/SettingModule/Views/Dialogs/SerialPortConfigView.xaml @@ -5,6 +5,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:SettingModule.Views.Dialogs" xmlns:i="http://schemas.microsoft.com/xaml/behaviors" + xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:helpers="clr-namespace:UIShare.Helpers;assembly=UIShare" xmlns:converters="clr-namespace:UIShare.Converters;assembly=UIShare" mc:Ignorable="d" @@ -14,20 +15,29 @@ Width="440" Height="430"> - + +