现场优化6

This commit is contained in:
2026-08-17 15:52:48 +08:00
parent 2bb8313f99
commit fb6e5b3cb1
61 changed files with 23694 additions and 146356 deletions
@@ -177,10 +177,15 @@ namespace TestingModule.ViewModels
private void SelectionChanged(object parameter)
{
var selectedList = parameter as IList;
if (selectedList != null)
if (parameter is IList list && list.Count > 0)
{
SelectedItems = selectedList.Cast<StepVM>().ToList();
SelectedItems = list.Cast<StepVM>().ToList();
}
else if (parameter is IEnumerable enumerable && parameter is not string)
{
var items = enumerable.Cast<StepVM>().ToList();
if (items.Count > 0)
SelectedItems = items;
}
}
@@ -199,13 +204,19 @@ namespace TestingModule.ViewModels
}
private void CopyStep()
{
if (_globalInfo.IsAdmin && SelectedItems.Any())
if (!_globalInfo.IsAdmin) return;
// 优先用多选列表,回退到单选
var source = (SelectedItems != null && SelectedItems.Any())
? SelectedItems
: (SelectedStep != null ? new List<StepVM> { SelectedStep } : null);
if (source == null || !source.Any()) return;
tmpCopyList.Clear();
foreach (var item in source)
{
tmpCopyList.Clear();
foreach (var item in SelectedItems)
{
tmpCopyList.Add(item);
}
tmpCopyList.Add(item);
}
}
private void PasteStep()
@@ -242,31 +253,29 @@ namespace TestingModule.ViewModels
}
private void DeleteStep()
{
// 确保有选中的项
if (_globalInfo.IsAdmin && SelectedItems != null && SelectedItems.Any())
if (!_globalInfo.IsAdmin) return;
// 优先用多选列表,回退到单选
var source = (SelectedItems != null && SelectedItems.Any())
? SelectedItems.ToList()
: (SelectedStep != null ? new List<StepVM> { SelectedStep } : null);
if (source == null || !source.Any()) return;
foreach (var item in source)
{
// 创建一个副本进行循环,防止在 Remove 过程中集合变化导致的问题
var toDelete = SelectedItems.ToList();
_eventAggregator.GetEvent<DeletedStepEvent>().Publish(item.ID);
foreach (var item in toDelete)
{
_eventAggregator.GetEvent<DeletedStepEvent>().Publish(item.ID);
if (_ScopedContext.SelectedStepList == "主程序")
{
Program.StepCollection.Remove(item);
}
else if (_ScopedContext.SelectedStepList == "错误程序")
{
Program.ErrorStepCollection.Remove(item);
}
}
// 3. 清空 ViewModel 的选中状态,避免悬挂引用
SelectedStep = null;
SelectedItems.Clear();
_ScopedContext.SelectedStep = null;
if (_ScopedContext.SelectedStepList == "主程序")
Program.StepCollection.Remove(item);
else if (_ScopedContext.SelectedStepList == "错误程序")
Program.ErrorStepCollection.Remove(item);
}
// 清空 ViewModel 的选中状态,避免悬挂引用
SelectedStep = null;
SelectedItems?.Clear();
_ScopedContext.SelectedStep = null;
}
#endregion
@@ -312,6 +321,7 @@ namespace TestingModule.ViewModels
/// </summary>
private void OnSubProgramNavigate(SubProgramNavigatePayload payload)
{
if (_ScopedContext == null || payload == null) return;
var nav = payload.IsErrorProgram ? _ScopedContext.ErrorNav : _ScopedContext.MainNav;
if (payload.Action == NavigateAction.Enter && payload.SubProgram != null)
{
@@ -438,9 +448,9 @@ namespace TestingModule.ViewModels
Program.PropertyChanged -= Program_PropertyChanged;
}
// 3. 【核心修复】必须显式退订 Prism 全局事件(AlarmEvent
// 注意:因为订阅时使用的是匿名 Lambda,最安全稳妥的退订方式是把整个事件上的当前 VM 订阅者全部注销
// 3. 显式退订全局 Prism 事件
_eventAggregator?.GetEvent<AlarmEvent>()?.Unsubscribe(null);
_eventAggregator?.GetEvent<SubProgramNavigateEvent>()?.Unsubscribe(null);
// 4. 清空临时缓存集合与 UI 绑定列表,避免悬挂指针
tmpCopyList?.Clear();
+8
View File
@@ -53,6 +53,10 @@
<i:InvokeCommandAction Command="{Binding SelectionChangedCommand}"
CommandParameter="{Binding SelectedItems, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
</i:EventTrigger>
<i:EventTrigger EventName="ContextMenuOpening">
<i:InvokeCommandAction Command="{Binding SelectionChangedCommand}"
CommandParameter="{Binding SelectedItems, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<!-- 行样式 -->
<DataGrid.RowStyle>
@@ -174,6 +178,10 @@
<i:InvokeCommandAction Command="{Binding SelectionChangedCommand}"
CommandParameter="{Binding SelectedItems, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
</i:EventTrigger>
<i:EventTrigger EventName="ContextMenuOpening">
<i:InvokeCommandAction Command="{Binding SelectionChangedCommand}"
CommandParameter="{Binding SelectedItems, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<!-- 行样式 -->
<DataGrid.RowStyle>