依赖注入顺序修改

This commit is contained in:
hsc
2026-06-11 10:42:00 +08:00
parent 420ca0ffd6
commit 9c661200b9
6 changed files with 27 additions and 32 deletions

View File

@@ -52,11 +52,22 @@ namespace MainModule.ViewModels
// 每个 AutomatedTestingViewModel 实例创建独立的容器作用域 // 每个 AutomatedTestingViewModel 实例创建独立的容器作用域
_scope = container.CreateScope(); _scope = container.CreateScope();
_globalInfo =container.Resolve<GlobalInfo>(); _globalInfo =container.Resolve<GlobalInfo>();
// 在该作用域内解析 ScopedContext —— 当前作用域唯一
_scopedContext = _scope.Resolve<ScopedContext>();
_stepRunning = _scope.Resolve<StepRunning>();
_systemConfig = _scope.Resolve<SystemConfig>(); _systemConfig = _scope.Resolve<SystemConfig>();
// 关键:从同一个 _scope 解析 5 个子 VMDI 会把同一个 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<ScopedContext>();
_deviceManager = _scope.Resolve<DeviceManager>();
_stepRunning = _scope.Resolve<StepRunning>();
// 从同一个 _scope 解析 5 个子 VMDI 会把同一个 ScopedContext 注入它们
CommandTreeVM = _scope.Resolve<CommandTreeViewModel>(); CommandTreeVM = _scope.Resolve<CommandTreeViewModel>();
StepsManagerVM = _scope.Resolve<StepsManagerViewModel>(); StepsManagerVM = _scope.Resolve<StepsManagerViewModel>();
SingleStepEditVM = _scope.Resolve<SingleStepEditViewModel>(); SingleStepEditVM = _scope.Resolve<SingleStepEditViewModel>();
@@ -111,17 +122,6 @@ namespace MainModule.ViewModels
if (navigationContext.Parameters.ContainsKey("Name")) if (navigationContext.Parameters.ContainsKey("Name"))
{ {
TestStatus = navigationContext.Parameters.GetValue<string>("Name"); TestStatus = navigationContext.Parameters.GetValue<string>("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.ContextDic.Add(TestStatus, _scopedContext);
_globalInfo.StepRunningDic.Add(TestStatus, _stepRunning); _globalInfo.StepRunningDic.Add(TestStatus, _stepRunning);
_globalInfo.ScopeDic.Add(TestStatus, _scope); _globalInfo.ScopeDic.Add(TestStatus, _scope);
@@ -146,7 +146,6 @@ namespace MainModule.ViewModels
_scopedContext.Program.StepCollection = program.StepCollection; _scopedContext.Program.StepCollection = program.StepCollection;
_scopedContext.Program.ErrorStepCollection = program.ErrorStepCollection; _scopedContext.Program.ErrorStepCollection = program.ErrorStepCollection;
_scopedContext.CurrentFilePath = filePath; _scopedContext.CurrentFilePath = filePath;
_deviceManager = _scope.Resolve<DeviceManager>();
} }
} }

View File

@@ -6,6 +6,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
using UIShare.GlobalVariable;
using UIShare.ViewModelBase; using UIShare.ViewModelBase;
namespace MainModule.ViewModels namespace MainModule.ViewModels
@@ -15,6 +16,7 @@ namespace MainModule.ViewModels
#region #region
private string _testStatus; private string _testStatus;
private string _moduleColor; private string _moduleColor;
private GlobalInfo _globalInfo;
#endregion #endregion
#region #region
@@ -24,11 +26,7 @@ namespace MainModule.ViewModels
set => SetProperty(ref _testStatus, value); set => SetProperty(ref _testStatus, value);
} }
public string ModuleColor
{
get => _moduleColor;
set => SetProperty(ref _moduleColor, value);
}
#endregion #endregion
#region #region
@@ -37,6 +35,7 @@ namespace MainModule.ViewModels
public ProtocolStartViewModel(IContainerProvider containerProvider) : base(containerProvider) public ProtocolStartViewModel(IContainerProvider containerProvider) : base(containerProvider)
{ {
_globalInfo = containerProvider.Resolve<GlobalInfo>();
StartProtocolCommand = new DelegateCommand(OnStart); StartProtocolCommand = new DelegateCommand(OnStart);
} }
@@ -69,8 +68,7 @@ namespace MainModule.ViewModels
// 2. 透传名称与颜色参数,使 AutomatedTestingViewModel 能正确初始化 // 2. 透传名称与颜色参数,使 AutomatedTestingViewModel 能正确初始化
var parameters = new NavigationParameters(); var parameters = new NavigationParameters();
parameters.Add("Name", TestStatus); parameters.Add("Name", TestStatus);
parameters.Add("Color", ModuleColor); _globalInfo.CurrentOpeningScope = TestStatus;
// 3. 在本格子 Region 内请求导航,导航成功后再移除旧的 ProtocolStartView 以释放资源 // 3. 在本格子 Region 内请求导航,导航成功后再移除旧的 ProtocolStartView 以释放资源
_regionManager.RequestNavigate(regionName, viewName, navResult => _regionManager.RequestNavigate(regionName, viewName, navResult =>
{ {
@@ -102,8 +100,7 @@ namespace MainModule.ViewModels
if (navigationContext.Parameters.ContainsKey("Name")) if (navigationContext.Parameters.ContainsKey("Name"))
TestStatus = navigationContext.Parameters.GetValue<string>("Name"); TestStatus = navigationContext.Parameters.GetValue<string>("Name");
if (navigationContext.Parameters.ContainsKey("Color"))
ModuleColor = navigationContext.Parameters.GetValue<string>("Color");
} }
#endregion #endregion
} }

View File

@@ -18,9 +18,7 @@ namespace UIShare.GlobalVariable
return false; return false;
} }
// 临时实例化一个对象以获取默认的 SystemPath string configPath = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ADP"), $"{title}.json");
var dummy = new SystemConfig();
string configPath = Path.Combine(dummy.SystemPath, $"{title}.json");
if (!File.Exists(configPath)) if (!File.Exists(configPath))
{ {

View File

@@ -17,7 +17,6 @@ namespace UIShare.GlobalVariable
public class DeviceManager public class DeviceManager
{ {
public SystemConfig _systemConfig { get; set; } public SystemConfig _systemConfig { get; set; }
public IList<IBaseInterface> DeviceList { get; private set; } = new List<IBaseInterface>();
/// <summary>按 DeviceName 索引的设备字典,便于业务层按名取实例。</summary> /// <summary>按 DeviceName 索引的设备字典,便于业务层按名取实例。</summary>
public IDictionary<string, IBaseInterface> DeviceMap { get; private set; } public IDictionary<string, IBaseInterface> DeviceMap { get; private set; }
@@ -30,6 +29,7 @@ namespace UIShare.GlobalVariable
{ {
_systemConfig = systemConfig; _systemConfig = systemConfig;
InitDevices(); InitDevices();
} }
/// <summary> /// <summary>
@@ -83,8 +83,6 @@ namespace UIShare.GlobalVariable
LoggerHelper.Warn($"设备 [{config.DeviceName}] 连接方式 [{config.ConnectionType}] 不支持,已跳过。"); LoggerHelper.Warn($"设备 [{config.DeviceName}] 连接方式 [{config.ConnectionType}] 不支持,已跳过。");
continue; continue;
} }
DeviceList.Add(instance);
if (!string.IsNullOrWhiteSpace(config.DeviceName)) if (!string.IsNullOrWhiteSpace(config.DeviceName))
{ {
DeviceMap[config.DeviceName] = instance; DeviceMap[config.DeviceName] = instance;

View File

@@ -15,6 +15,7 @@ namespace UIShare.GlobalVariable
public Dictionary<string, IScopedProvider> ScopeDic { get; set; } public Dictionary<string, IScopedProvider> ScopeDic { get; set; }
public String UserName { get; set; } = "Not Logged in"; public String UserName { get; set; } = "Not Logged in";
public bool IsAdmin { get; set; } = true; public bool IsAdmin { get; set; } = true;
public string CurrentOpeningScope;
private string _currentScope = "default"; private string _currentScope = "default";
public string CurrentScope public string CurrentScope
{ {

View File

@@ -22,6 +22,7 @@ namespace UIShare
{ {
private ScopedContext _scopedContext; private ScopedContext _scopedContext;
private SystemConfig _systemConfig; private SystemConfig _systemConfig;
private DeviceManager _deviceManager;
//private Devices _devices; //private Devices _devices;
private IContainerProvider containerProvider; private IContainerProvider containerProvider;
private IEventAggregator _eventAggregator; private IEventAggregator _eventAggregator;
@@ -39,11 +40,12 @@ namespace UIShare
private bool SubSingleStep = false; private bool SubSingleStep = false;
private Guid TestRoundID; 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; _scopedContext = ScopedContext;
_systemConfig = systemConfig; _systemConfig = systemConfig;
_eventAggregator = eventAggregator; _eventAggregator = eventAggregator;
_deviceManager= deviceManager;
//_devices = containerProvider.Resolve<Devices>(); //_devices = containerProvider.Resolve<Devices>();
} }
public async Task<bool> ExecuteErrorSteps(ProgramVM program, int depth = 0, CancellationToken cancellationToken = default) public async Task<bool> ExecuteErrorSteps(ProgramVM program, int depth = 0, CancellationToken cancellationToken = default)