依赖注入顺序修改

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 实例创建独立的容器作用域
_scope = container.CreateScope();
_globalInfo =container.Resolve<GlobalInfo>();
// 在该作用域内解析 ScopedContext —— 当前作用域唯一
_scopedContext = _scope.Resolve<ScopedContext>();
_stepRunning = _scope.Resolve<StepRunning>();
_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>();
StepsManagerVM = _scope.Resolve<StepsManagerViewModel>();
SingleStepEditVM = _scope.Resolve<SingleStepEditViewModel>();
@@ -111,17 +122,6 @@ namespace MainModule.ViewModels
if (navigationContext.Parameters.ContainsKey("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.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<DeviceManager>();
}
}

View File

@@ -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<GlobalInfo>();
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<string>("Name");
if (navigationContext.Parameters.ContainsKey("Color"))
ModuleColor = navigationContext.Parameters.GetValue<string>("Color");
}
#endregion
}