127 lines
4.9 KiB
C#
127 lines
4.9 KiB
C#
using System;
|
||
using System.Windows.Input;
|
||
using Prism.Ioc;
|
||
using TestingModule.ViewModels;
|
||
using UIShare;
|
||
using UIShare.GlobalVariable;
|
||
using UIShare.PubEvent;
|
||
using UIShare.ViewModelBase;
|
||
|
||
namespace MainModule.ViewModels
|
||
{
|
||
public class AutomatedTestingViewModel : NavigateViewModelBase, IRegionMemberLifetime, IDisposable
|
||
{
|
||
#region 私有字段
|
||
private string _testStatus;
|
||
private readonly IScopedProvider _scope;
|
||
#endregion
|
||
|
||
#region 属性
|
||
public bool KeepAlive => true; // 保持存活
|
||
|
||
public string TestStatus
|
||
{
|
||
get => _testStatus;
|
||
set => SetProperty(ref _testStatus, value);
|
||
}
|
||
|
||
// 该 AutomatedTestingView 实例独占的 ScopedContext,5 个子面板共享
|
||
public ScopedContext _scopedContext { get; }
|
||
public StepRunning _stepRunning { get; }
|
||
public GlobalInfo _globalInfo { get; }
|
||
public SystemConfig _systemConfig { get; set; }
|
||
|
||
// 5 个子 ViewModel 全部从同一个 scope 解析,自动注入同一个 ScopedContext
|
||
public CommandTreeViewModel CommandTreeVM { get; }
|
||
public StepsManagerViewModel StepsManagerVM { get; }
|
||
public SingleStepEditViewModel SingleStepEditVM { get; }
|
||
public LogAreaViewModel LogAreaVM { get; }
|
||
public ParametersManagerViewModel ParametersManagerVM { get; }
|
||
#endregion
|
||
|
||
public ICommand RefreshCommand { get; set; }
|
||
public ICommand BackToProtocolCommand { get; set; }
|
||
|
||
public AutomatedTestingViewModel(IContainerExtension container) : base(container)
|
||
{
|
||
// 每个 AutomatedTestingViewModel 实例创建独立的容器作用域
|
||
_scope = container.CreateScope();
|
||
_globalInfo=container.Resolve<GlobalInfo>();
|
||
// 在该作用域内解析 ScopedContext —— 当前作用域唯一
|
||
_scopedContext = _scope.Resolve<ScopedContext>();
|
||
_stepRunning = _scope.Resolve<StepRunning>();
|
||
// 关键:从同一个 _scope 解析 5 个子 VM,DI 会把同一个 ScopedContext 注入它们
|
||
CommandTreeVM = _scope.Resolve<CommandTreeViewModel>();
|
||
StepsManagerVM = _scope.Resolve<StepsManagerViewModel>();
|
||
SingleStepEditVM = _scope.Resolve<SingleStepEditViewModel>();
|
||
LogAreaVM = _scope.Resolve<LogAreaViewModel>();
|
||
ParametersManagerVM = _scope.Resolve<ParametersManagerViewModel>();
|
||
RefreshCommand = new DelegateCommand(OnRefresh);
|
||
BackToProtocolCommand = new DelegateCommand(OnBackToProtocol);
|
||
}
|
||
|
||
public void Dispose()
|
||
{
|
||
if (string.IsNullOrEmpty(TestStatus))
|
||
{
|
||
_scope?.Dispose();
|
||
return;
|
||
}
|
||
try
|
||
{
|
||
_globalInfo.ContextDic?.Remove(TestStatus);
|
||
_globalInfo.StepRunningDic?.Remove(TestStatus);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Logger.LoggerHelper.ErrorWithNotify($"卸载机台 [{TestStatus}] 全局引用失败: {ex.Message}");
|
||
}
|
||
finally
|
||
{
|
||
_scope?.Dispose();
|
||
}
|
||
}
|
||
#region 命令处理与事件
|
||
private void OnRefresh()
|
||
{
|
||
// 双击:把自己的名字扔出去
|
||
_globalInfo.CurrentScope = TestStatus;
|
||
_eventAggregator.GetEvent<ExpandViewEvent>().Publish(TestStatus);
|
||
|
||
}
|
||
|
||
private void OnBackToProtocol()
|
||
{
|
||
// 返回:扔个空字符串出去
|
||
_eventAggregator.GetEvent<ExpandViewEvent>().Publish("");
|
||
}
|
||
#endregion
|
||
|
||
#region 重写
|
||
public override void OnNavigatedTo(NavigationContext navigationContext)
|
||
{
|
||
base.OnNavigatedTo(navigationContext);
|
||
if (navigationContext.Parameters.ContainsKey("Name"))
|
||
{
|
||
TestStatus = navigationContext.Parameters.GetValue<string>("Name");
|
||
_globalInfo.ContextDic.Add(TestStatus, _scopedContext);
|
||
_globalInfo.StepRunningDic.Add(TestStatus, _stepRunning);
|
||
_globalInfo.ScopeDic.Add(TestStatus, _scope);
|
||
_systemConfig = _scope.Resolve<SystemConfig>();
|
||
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);
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
#endregion
|
||
}
|
||
} |