From 03e92811dd23b79e3637a4d09b0f5131ad4ed3ec Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Wed, 1 Oct 2025 18:02:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=A0=E9=99=A4=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E6=97=B6=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DMS.Application/Interfaces/IEventService.cs | 12 ++++++++++++ DMS.Application/Services/EventService.cs | 15 +++++++++++++++ DMS.WPF/Services/DeviceDataService.cs | 6 +++++- DMS.WPF/ViewModels/DevicesViewModel.cs | 1 + 4 files changed, 33 insertions(+), 1 deletion(-) 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) {