修复删除设备时的错误

This commit is contained in:
2025-10-01 18:02:33 +08:00
parent eb4bee8963
commit 03e92811dd
4 changed files with 33 additions and 1 deletions

View File

@@ -23,6 +23,18 @@ public interface IEventService
/// <param name="e">设备状态改变事件参数</param>
void RaiseDeviceActiveChanged(object sender, DeviceActiveChangedEventArgs e);
/// <summary>
/// 设备添加事件
/// </summary>
event EventHandler<DeviceChangedEventArgs> OnDeviceChanged;
/// <summary>
/// 触发设备添加事件
/// </summary>
/// <param name="sender">事件发送者</param>
/// <param name="e">设备变更事件参数</param>
void RaiseDeviceChanged(object sender, DeviceChangedEventArgs e);
#endregion
#region

View File

@@ -29,6 +29,11 @@ public class EventService : IEventService
/// </summary>
public event EventHandler<DeviceConnectChangedEventArgs> OnDeviceConnectChanged;
/// <summary>
/// 设备添加事件
/// </summary>
public event EventHandler<DeviceChangedEventArgs> OnDeviceChanged;
/// <summary>
/// 触发设备状态改变事件
@@ -58,6 +63,16 @@ public class EventService : IEventService
}
/// <summary>
/// 触发设备添加事件
/// </summary>
/// <param name="sender">事件发送者</param>
/// <param name="e">设备变更事件参数</param>
public void RaiseDeviceChanged(object sender, DeviceChangedEventArgs e)
{
OnDeviceChanged?.Invoke(sender, e);
}
#endregion
#region

View File

@@ -3,6 +3,7 @@ using System.Windows.Threading;
using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel;
using DMS.Application.DTOs;
using DMS.Application.Events;
using DMS.Application.Interfaces;
using DMS.Core.Enums;
using DMS.Core.Events;
@@ -133,6 +134,7 @@ public class DeviceDataService : IDeviceDataService
// 添加null检查
_menuDataService.BuildMenuTrees();
return addDto;
}
@@ -152,7 +154,8 @@ public class DeviceDataService : IDeviceDataService
// 从界面删除设备相关数据集
foreach (var variableTable in device.VariableTables)
var variableTablesCopy = device.VariableTables.ToList();
foreach (var variableTable in variableTablesCopy)
{
await _variableTableDataService.DeleteVariableTable(variableTable);
}
@@ -164,6 +167,7 @@ public class DeviceDataService : IDeviceDataService
}
_dataStorageService.Devices.Remove(device.Id);
return true;
}

View File

@@ -137,6 +137,7 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable
if (addDto != null && addDto.Device != null && _notificationService != null)
{
_notificationService.ShowSuccess($"设备添加成功:{addDto.Device.Name}");
}
else if (_notificationService != null)
{