diff --git a/DMS.Application/Interfaces/IEventService.cs b/DMS.Application/Interfaces/IEventService.cs index b76005f..76e49b3 100644 --- a/DMS.Application/Interfaces/IEventService.cs +++ b/DMS.Application/Interfaces/IEventService.cs @@ -23,6 +23,18 @@ public interface IEventService /// 设备状态改变事件参数 void RaiseDeviceActiveChanged(object sender, DeviceActiveChangedEventArgs e); + /// + /// 设备添加事件 + /// + event EventHandler OnDeviceChanged; + + /// + /// 触发设备添加事件 + /// + /// 事件发送者 + /// 设备变更事件参数 + void RaiseDeviceChanged(object sender, DeviceChangedEventArgs e); + #endregion #region 变量事件 diff --git a/DMS.Application/Services/EventService.cs b/DMS.Application/Services/EventService.cs index 0ef7b7a..4ff1778 100644 --- a/DMS.Application/Services/EventService.cs +++ b/DMS.Application/Services/EventService.cs @@ -29,6 +29,11 @@ public class EventService : IEventService /// public event EventHandler OnDeviceConnectChanged; + /// + /// 设备添加事件 + /// + public event EventHandler OnDeviceChanged; + /// /// 触发设备状态改变事件 @@ -58,6 +63,16 @@ public class EventService : IEventService } + /// + /// 触发设备添加事件 + /// + /// 事件发送者 + /// 设备变更事件参数 + public void RaiseDeviceChanged(object sender, DeviceChangedEventArgs e) + { + OnDeviceChanged?.Invoke(sender, e); + } + #endregion #region 变量事件 diff --git a/DMS.WPF/Services/DeviceDataService.cs b/DMS.WPF/Services/DeviceDataService.cs index 512dfe1..269b929 100644 --- a/DMS.WPF/Services/DeviceDataService.cs +++ b/DMS.WPF/Services/DeviceDataService.cs @@ -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; @@ -132,6 +133,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); } @@ -163,6 +166,7 @@ public class DeviceDataService : IDeviceDataService _menuDataService.DeleteMenuItem(deviceMenu); } _dataStorageService.Devices.Remove(device.Id); + return true; } diff --git a/DMS.WPF/ViewModels/DevicesViewModel.cs b/DMS.WPF/ViewModels/DevicesViewModel.cs index 95c967d..dcb484d 100644 --- a/DMS.WPF/ViewModels/DevicesViewModel.cs +++ b/DMS.WPF/ViewModels/DevicesViewModel.cs @@ -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) {