From 9d446c370e81f5f69bca4b3cda5a2e7273506c86 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Thu, 4 Sep 2025 19:59:35 +0800 Subject: [PATCH] =?UTF-8?q?=20=E5=B0=86NotificationService=E6=8A=BD?= =?UTF-8?q?=E5=8F=96=E6=88=90=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=B9=B6=E5=B0=86?= =?UTF-8?q?=E6=89=80=E6=9C=89=E4=BD=BF=E7=94=A8NotificationService?= =?UTF-8?q?=E5=85=A8=E9=83=A8=E6=9B=BF=E6=8D=A2=E4=B8=BA=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?INotificationService?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DMS.WPF/App.xaml.cs | 4 +- DMS.WPF/Interfaces/INotificationService.cs | 72 ++++++++++++++++++- DMS.WPF/Services/NavigationService.cs | 12 +--- DMS.WPF/Services/NotificationService.cs | 3 +- DMS.WPF/ViewModels/DeviceDetailViewModel.cs | 4 +- DMS.WPF/ViewModels/DevicesViewModel.cs | 4 +- .../Dialogs/ImportExcelDialogViewModel.cs | 5 +- .../Dialogs/ImportOpcUaDialogViewModel.cs | 5 +- .../ViewModels/MqttServerDetailViewModel.cs | 4 +- DMS.WPF/ViewModels/VariableTableViewModel.cs | 7 +- .../Views/Dialogs/ImportOpcUaDialog.xaml.cs | 3 +- DMS.WPF/Views/VariableTableView.xaml.cs | 3 +- 12 files changed, 95 insertions(+), 31 deletions(-) diff --git a/DMS.WPF/App.xaml.cs b/DMS.WPF/App.xaml.cs index e237c24..2c16552 100644 --- a/DMS.WPF/App.xaml.cs +++ b/DMS.WPF/App.xaml.cs @@ -75,7 +75,7 @@ public partial class App : System.Windows.Application } catch (Exception exception) { - var notificationService = Host.Services.GetRequiredService(); + var notificationService = Host.Services.GetRequiredService(); notificationService.ShowError($"加载数据时发生错误,如果是连接字符串不正确,可以在设置界面更改:{exception.Message}", exception); } @@ -109,7 +109,7 @@ public partial class App : System.Windows.Application services.AddSingleton(); // services.AddSingleton(); - services.AddSingleton(); + services.AddSingleton(); // services.AddHostedService(); // services.AddHostedService(); // services.AddHostedService(); diff --git a/DMS.WPF/Interfaces/INotificationService.cs b/DMS.WPF/Interfaces/INotificationService.cs index 39514c8..44b6c9b 100644 --- a/DMS.WPF/Interfaces/INotificationService.cs +++ b/DMS.WPF/Interfaces/INotificationService.cs @@ -1,7 +1,73 @@ -namespace DMS.WPF.Interfaces; +using System; +using System.Runtime.CompilerServices; +using DMS.Core.Enums; + +namespace DMS.WPF.Interfaces; public interface INotificationService { - // void Show(Notification notification); - // void Show(string message, NotificationType type = NotificationType.Info, bool IsGlobal = true); + /// + /// 显示一个通用通知消息,并根据通知类型记录日志。支持节流。 + /// + /// 通知消息内容。 + /// 通知类型(如信息、错误、成功等)。 + /// 是否启用通知节流。 + /// 自动捕获:调用此方法的源文件完整路径。 + /// 自动捕获:调用此方法的行号。 + void ShowMessage(string msg, NotificationType notificationType = NotificationType.Info, bool throttle = true, + [CallerFilePath] string callerFilePath = "", + [CallerLineNumber] int callerLineNumber = 0); + + /// + /// 显示一个错误通知消息,并记录错误日志。支持节流。 + /// + /// 错误消息内容。 + /// 可选:要记录的异常对象。 + /// 是否启用通知和日志节流。 + /// 自动捕获:调用此方法的源文件完整路径。 + /// 自动捕获:调用此方法的成员或属性名称。 + /// 自动捕获:调用此方法的行号。 + void ShowError(string msg, Exception exception = null, bool throttle = true, + [CallerFilePath] string callerFilePath = "", + [CallerMemberName] string callerMember = "", + [CallerLineNumber] int callerLineNumber = 0); + + /// + /// 显示一个成功通知消息,并记录信息日志。支持节流。 + /// + /// 成功消息内容。 + /// 是否启用通知和日志节流。 + /// 自动捕获:调用此方法的源文件完整路径。 + /// 自动捕获:调用此方法的成员或属性名称。 + /// 自动捕获:调用此方法的行号。 + void ShowSuccess(string msg, bool throttle = true, + [CallerFilePath] string callerFilePath = "", + [CallerMemberName] string callerMember = "", + [CallerLineNumber] int callerLineNumber = 0); + + /// + /// 显示一个信息通知消息,并记录信息日志。支持节流。 + /// + /// 信息消息内容。 + /// 是否启用通知和日志节流。 + /// 自动捕获:调用此方法的源文件完整路径。 + /// 自动捕获:调用此方法的成员或属性名称。 + /// 自动捕获:调用此方法的行号。 + void ShowInfo(string msg, bool throttle = true, + [CallerFilePath] string callerFilePath = "", + [CallerMemberName] string callerMember = "", + [CallerLineNumber] int callerLineNumber = 0); + + /// + /// 显示一个警告通知消息,并记录警告日志。支持节流。 + /// + /// 警告消息内容。 + /// 是否启用通知和日志节流。 + /// 自动捕获:调用此方法的源文件完整路径。 + /// 自动捕获:调用此方法的成员或属性名称。 + /// 自动捕获:调用此方法的行号。 + void ShowWarn(string msg, bool throttle = true, + [CallerFilePath] string callerFilePath = "", + [CallerMemberName] string callerMember = "", + [CallerLineNumber] int callerLineNumber = 0); } \ No newline at end of file diff --git a/DMS.WPF/Services/NavigationService.cs b/DMS.WPF/Services/NavigationService.cs index 9201cce..42fc347 100644 --- a/DMS.WPF/Services/NavigationService.cs +++ b/DMS.WPF/Services/NavigationService.cs @@ -1,16 +1,8 @@ -// 文件: DMS.WPF/Services/NavigationService.cs - using DMS.ViewModels; +using DMS.WPF.Interfaces; using DMS.WPF.ViewModels; using DMS.WPF.ViewModels.Items; -using DMS.WPF.Views; using Microsoft.Extensions.DependencyInjection; -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using System.Windows; -using DMS.WPF.Interfaces; -using DMS.WPF.Services; namespace DMS.WPF.Services; @@ -43,7 +35,7 @@ public class NavigationService : INavigationService var viewModel = GetViewModelByKey(menu.TargetViewKey); if (viewModel == null) { - var notificationService = App.Current.Services.GetRequiredService(); + var notificationService = App.Current.Services.GetRequiredService(); notificationService.ShowError($"切换界面失败,没有找到界面:{menu.TargetViewKey}"); return; } diff --git a/DMS.WPF/Services/NotificationService.cs b/DMS.WPF/Services/NotificationService.cs index 5342462..79935bf 100644 --- a/DMS.WPF/Services/NotificationService.cs +++ b/DMS.WPF/Services/NotificationService.cs @@ -5,6 +5,7 @@ using System.Threading; using CommunityToolkit.Mvvm.Messaging; using DMS.Core.Enums; using DMS.Message; +using DMS.WPF.Interfaces; using Microsoft.Extensions.Logging; namespace DMS.WPF.Services; @@ -13,7 +14,7 @@ namespace DMS.WPF.Services; /// 通知服务类,用于显示各种类型的通知消息,并集成日志记录功能。 /// 新增了通知节流功能,以防止在短时间内向用户发送大量重复的通知。 /// -public class NotificationService +public class NotificationService : INotificationService { private readonly ILogger _logger; diff --git a/DMS.WPF/ViewModels/DeviceDetailViewModel.cs b/DMS.WPF/ViewModels/DeviceDetailViewModel.cs index d305c9f..a4e05e0 100644 --- a/DMS.WPF/ViewModels/DeviceDetailViewModel.cs +++ b/DMS.WPF/ViewModels/DeviceDetailViewModel.cs @@ -27,10 +27,10 @@ public partial class DeviceDetailViewModel : ViewModelBase, INavigatable [ObservableProperty] private VariableTableItemViewModel _selectedVariableTable; - private readonly NotificationService _notificationService; + private readonly INotificationService _notificationService; public DeviceDetailViewModel(IMapper mapper, IDialogService dialogService, INavigationService navigationService, - DataServices dataServices, NotificationService notificationService) + DataServices dataServices, INotificationService notificationService) { _mapper = mapper; _dialogService = dialogService; diff --git a/DMS.WPF/ViewModels/DevicesViewModel.cs b/DMS.WPF/ViewModels/DevicesViewModel.cs index 9b11bf7..8f6e98b 100644 --- a/DMS.WPF/ViewModels/DevicesViewModel.cs +++ b/DMS.WPF/ViewModels/DevicesViewModel.cs @@ -38,7 +38,7 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable [ObservableProperty] private DeviceItemViewModel _selectedDevice; - private readonly NotificationService _notificationService; + private readonly INotificationService _notificationService; /// /// 初始化 类的新实例。 @@ -49,7 +49,7 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable public DevicesViewModel(IMapper mapper, IDialogService dialogService, INavigationService navigationService, DataServices dataServices, IDeviceAppService deviceAppService, - NotificationService notificationService) + INotificationService notificationService) { _mapper = mapper; _dialogService = dialogService; diff --git a/DMS.WPF/ViewModels/Dialogs/ImportExcelDialogViewModel.cs b/DMS.WPF/ViewModels/Dialogs/ImportExcelDialogViewModel.cs index 73c2b2e..d4f0ba4 100644 --- a/DMS.WPF/ViewModels/Dialogs/ImportExcelDialogViewModel.cs +++ b/DMS.WPF/ViewModels/Dialogs/ImportExcelDialogViewModel.cs @@ -6,6 +6,7 @@ using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using DMS.Core.Interfaces.Services; using DMS.Core.Models; +using DMS.WPF.Interfaces; using DMS.WPF.Services; using DMS.WPF.ViewModels.Items; @@ -15,7 +16,7 @@ public partial class ImportExcelDialogViewModel : DialogViewModelBase /// 通知服务实例 /// - private readonly NotificationService _notificationService; + private readonly INotificationService _notificationService; /// /// 构造函数 @@ -101,7 +102,7 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBaseOPC UA服务接口实例 /// 对象映射器实例 /// 通知服务实例 - public ImportOpcUaDialogViewModel(IOpcUaService opcUaService, IMapper mapper, NotificationService notificationService) + public ImportOpcUaDialogViewModel(IOpcUaService opcUaService, IMapper mapper, INotificationService notificationService) { _opcUaService = opcUaService; _mapper = mapper; diff --git a/DMS.WPF/ViewModels/MqttServerDetailViewModel.cs b/DMS.WPF/ViewModels/MqttServerDetailViewModel.cs index 0f29f9e..ad7217e 100644 --- a/DMS.WPF/ViewModels/MqttServerDetailViewModel.cs +++ b/DMS.WPF/ViewModels/MqttServerDetailViewModel.cs @@ -20,7 +20,7 @@ namespace DMS.ViewModels private readonly ILogger _logger; private readonly DataServices _dataServices; private readonly IDialogService _dialogService; - private readonly NotificationService _notificationService; + private readonly INotificationService _notificationService; /// /// 当前正在编辑的MQTT服务器对象。 @@ -43,7 +43,7 @@ namespace DMS.ViewModels /// 对话框服务。 /// 通知服务。 public MqttServerDetailViewModel(ILogger logger, DataServices dataServices, - IDialogService dialogService, NotificationService notificationService) + IDialogService dialogService, INotificationService notificationService) { _logger = logger; _dataServices = dataServices; diff --git a/DMS.WPF/ViewModels/VariableTableViewModel.cs b/DMS.WPF/ViewModels/VariableTableViewModel.cs index b6d0327..9728828 100644 --- a/DMS.WPF/ViewModels/VariableTableViewModel.cs +++ b/DMS.WPF/ViewModels/VariableTableViewModel.cs @@ -5,6 +5,7 @@ using DMS.Application.DTOs; using DMS.Application.Interfaces; using DMS.Core.Enums; using DMS.Core.Models; +using DMS.WPF.Interfaces; using DMS.WPF.Services; using DMS.WPF.ViewModels.Dialogs; using DMS.WPF.ViewModels.Items; @@ -13,7 +14,6 @@ using ObservableCollections; using System.Collections; using System.Collections.Generic; using System.Linq; -using DMS.WPF.Interfaces; namespace DMS.WPF.ViewModels; @@ -86,10 +86,10 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable private readonly ISynchronizedView _synchronizedView; public NotifyCollectionChangedSynchronizedViewList VariableItemListView { get; } - private readonly NotificationService _notificationService; + private readonly INotificationService _notificationService; public VariableTableViewModel(IMapper mapper, IDialogService dialogService, IVariableAppService variableAppService, - DataServices dataServices, NotificationService notificationService) + DataServices dataServices, INotificationService notificationService) { _mapper = mapper; _dialogService = dialogService; @@ -198,6 +198,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable // 更新数据库中的变量数据 var updateResult = await _variableAppService.UpdateVariableAsync(_mapper.Map(editedVariable)); + if (updateResult > 0) { // 更新当前页面显示的数据:找到原数据在集合中的索引并替换 diff --git a/DMS.WPF/Views/Dialogs/ImportOpcUaDialog.xaml.cs b/DMS.WPF/Views/Dialogs/ImportOpcUaDialog.xaml.cs index be2770c..6c19c0c 100644 --- a/DMS.WPF/Views/Dialogs/ImportOpcUaDialog.xaml.cs +++ b/DMS.WPF/Views/Dialogs/ImportOpcUaDialog.xaml.cs @@ -1,5 +1,6 @@ using DMS.Services; using DMS.WPF.Helper; +using DMS.WPF.Interfaces; using DMS.WPF.Services; using DMS.WPF.ViewModels.Dialogs; using DMS.WPF.ViewModels.Items; @@ -52,7 +53,7 @@ public partial class ImportOpcUaDialog : ContentDialog } catch (Exception ex) { - var notificationService = App.Current.Services.GetRequiredService(); + var notificationService = App.Current.Services.GetRequiredService(); notificationService.ShowError($"选择节点时发生了错误:{ex.Message}"); } diff --git a/DMS.WPF/Views/VariableTableView.xaml.cs b/DMS.WPF/Views/VariableTableView.xaml.cs index 00cb79f..0e4aadd 100644 --- a/DMS.WPF/Views/VariableTableView.xaml.cs +++ b/DMS.WPF/Views/VariableTableView.xaml.cs @@ -3,6 +3,7 @@ using System.Windows.Controls; using System.Windows.Data; using System.Windows.Media; using DMS.WPF.ViewModels; +using DMS.WPF.Interfaces; using DMS.WPF.Services; using iNKORE.UI.WPF.Modern.Controls; using Microsoft.Extensions.DependencyInjection; @@ -39,7 +40,7 @@ public partial class VariableTableView : UserControl } catch (Exception exception) { - var notificationService = App.Current.Services.GetRequiredService(); + var notificationService = App.Current.Services.GetRequiredService(); notificationService.ShowError($"修改变量表启用,停用时发生了错误:{exception.Message}", exception); } }