添加项目文件。
This commit is contained in:
114
MainModule/ViewModels/AutomatedTestingViewModel.cs
Normal file
114
MainModule/ViewModels/AutomatedTestingViewModel.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
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; }
|
||||
|
||||
// 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>();
|
||||
_systemConfig=_scope.Resolve<SystemConfig>();
|
||||
// 关键:从同一个 _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()
|
||||
{
|
||||
// 双击:把自己的名字扔出去
|
||||
_eventAggregator.GetEvent<ExpandViewEvent>().Publish(TestStatus);
|
||||
_globalInfo.CurrentScope = 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);
|
||||
}
|
||||
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
67
MainModule/ViewModels/MainViewModel.cs
Normal file
67
MainModule/ViewModels/MainViewModel.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using UIShare.PubEvent;
|
||||
using MainModule.ViewModels;
|
||||
using UIShare.ViewModelBase;
|
||||
|
||||
|
||||
namespace MainModule.ViewModels
|
||||
{
|
||||
public class MainViewModel : NavigateViewModelBase, IRegionMemberLifetime
|
||||
{
|
||||
#region 私有字段
|
||||
private bool IsInitialized = false;
|
||||
private string _expandedCellName = string.Empty;
|
||||
#endregion
|
||||
|
||||
#region 属性
|
||||
public bool KeepAlive => true; // 保持存活
|
||||
|
||||
public string ExpandedCellName
|
||||
{
|
||||
get => _expandedCellName;
|
||||
set => SetProperty(ref _expandedCellName, value);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 命令
|
||||
public ICommand LoadedCommand { get; set; }
|
||||
#endregion
|
||||
|
||||
public MainViewModel(IContainerProvider containerProvider) : base(containerProvider)
|
||||
{
|
||||
LoadedCommand = new DelegateCommand(OnLoaded);
|
||||
_eventAggregator.GetEvent<ExpandViewEvent>().Subscribe(OnCellExpandRequested);
|
||||
}
|
||||
|
||||
#region 命令处理与事件
|
||||
private void OnLoaded()
|
||||
{
|
||||
if (IsInitialized) return;
|
||||
for (int i = 1; i <= 9; i++)
|
||||
{
|
||||
var parameters = new NavigationParameters { { "Name", $"TestCell{i}" } };
|
||||
_regionManager.RequestNavigate($"TestCell{i}", "ProtocolStartView", parameters);
|
||||
}
|
||||
IsInitialized = true;
|
||||
}
|
||||
|
||||
// 不再物理搬迁视图,只需修改一个字符串属性 ExpandedCellName,
|
||||
// XAML 里每个单元的 Style.Triggers 会根据该值处理隐藏 / 跨越 3x3。
|
||||
private void OnCellExpandRequested(string cellName)
|
||||
{
|
||||
ExpandedCellName = cellName ?? string.Empty;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 重写
|
||||
public override bool IsNavigationTarget(NavigationContext navigationContext)
|
||||
{
|
||||
// 之前帮你改好的单例复用逻辑(防止重复 new 和初始化视图)
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
109
MainModule/ViewModels/ProtocolStartViewModel.cs
Normal file
109
MainModule/ViewModels/ProtocolStartViewModel.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using Prism.Navigation.Regions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using UIShare.ViewModelBase;
|
||||
|
||||
namespace MainModule.ViewModels
|
||||
{
|
||||
public class ProtocolStartViewModel : NavigateViewModelBase
|
||||
{
|
||||
#region 私有字段
|
||||
private string _testStatus;
|
||||
private string _moduleColor;
|
||||
#endregion
|
||||
|
||||
#region 属性
|
||||
public string TestStatus
|
||||
{
|
||||
get => _testStatus;
|
||||
set => SetProperty(ref _testStatus, value);
|
||||
}
|
||||
|
||||
public string ModuleColor
|
||||
{
|
||||
get => _moduleColor;
|
||||
set => SetProperty(ref _moduleColor, value);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 命令
|
||||
public DelegateCommand StartProtocolCommand { get; }
|
||||
#endregion
|
||||
|
||||
public ProtocolStartViewModel(IContainerProvider containerProvider) : base(containerProvider)
|
||||
{
|
||||
StartProtocolCommand = new DelegateCommand(OnStart);
|
||||
}
|
||||
|
||||
#region 命令处理与事件
|
||||
private void OnStart()
|
||||
{
|
||||
// 只在当前格子所属的 Cell Region 内完成切换,不会影响其他格子位置
|
||||
SwitchNavigate("AutomatedTestingView");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 定位当前视图所在的 Cell Region(TestCell1..TestCell9),
|
||||
/// 在同一个 Region 内完成跳转。Region 位置由 XAML Grid 锁定,永远不会错位。
|
||||
/// </summary>
|
||||
public void SwitchNavigate(string viewName)
|
||||
{
|
||||
// 1. 反向查找:哪个 Cell Region 当前托着“我”这个 ProtocolStartView
|
||||
for (int i = 1; i <= 9; i++)
|
||||
{
|
||||
var regionName = $"TestCell{i}";
|
||||
if (!_regionManager.Regions.ContainsRegionWithName(regionName)) continue;
|
||||
|
||||
var region = _regionManager.Regions[regionName];
|
||||
var myView = region.Views
|
||||
.OfType<FrameworkElement>()
|
||||
.FirstOrDefault(v => v.DataContext == this);
|
||||
|
||||
if (myView == null) continue;
|
||||
|
||||
// 2. 透传名称与颜色参数,使 AutomatedTestingViewModel 能正确初始化
|
||||
var parameters = new NavigationParameters();
|
||||
parameters.Add("Name", TestStatus);
|
||||
parameters.Add("Color", ModuleColor);
|
||||
|
||||
// 3. 在本格子 Region 内请求导航,导航成功后再移除旧的 ProtocolStartView 以释放资源
|
||||
_regionManager.RequestNavigate(regionName, viewName, navResult =>
|
||||
{
|
||||
if (navResult.Success == true)
|
||||
{
|
||||
region.Remove(myView);
|
||||
}
|
||||
}, parameters);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 重写
|
||||
public override bool IsNavigationTarget(NavigationContext navigationContext)
|
||||
{
|
||||
if (navigationContext.Parameters.ContainsKey("Name"))
|
||||
return TestStatus == navigationContext.Parameters.GetValue<string>("Name");
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void OnNavigatedTo(NavigationContext navigationContext)
|
||||
{
|
||||
base.OnNavigatedTo(navigationContext);
|
||||
|
||||
// 接收导航传参:名称与颜色
|
||||
if (navigationContext.Parameters.ContainsKey("Name"))
|
||||
TestStatus = navigationContext.Parameters.GetValue<string>("Name");
|
||||
|
||||
if (navigationContext.Parameters.ContainsKey("Color"))
|
||||
ModuleColor = navigationContext.Parameters.GetValue<string>("Color");
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user