From 0980f84d096213eef8e51486eee2f4a4f8cb4496 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Sun, 6 Jul 2025 15:36:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=20NotificationHelper.ShowMessage?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E5=85=B7=E4=BD=93=E7=9A=84ShowError=E5=BE=97?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml.cs | 4 ++-- Data/Repositories/DeviceRepository.cs | 4 +--- Services/DataServices.cs | 3 +-- Services/MqttBackgroundService.cs | 6 +++--- Services/NavgatorServices.cs | 3 +-- ViewModels/DevicesViewModel.cs | 6 +++--- ViewModels/MainViewModel.cs | 8 ++++---- ViewModels/MqttServerDetailViewModel.cs | 8 ++++---- ViewModels/MqttsViewModel.cs | 6 +++--- ViewModels/SettingViewModel.cs | 4 ++-- ViewModels/VariableTableViewModel.cs | 26 ++++++++++++------------- Views/MainView.xaml.cs | 2 +- Views/VariableTableView.xaml.cs | 20 +++++++------------ 13 files changed, 45 insertions(+), 55 deletions(-) diff --git a/App.xaml.cs b/App.xaml.cs index a621e74..d22c1a0 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -53,13 +53,13 @@ public partial class App : Application { InitializeDataBase(); InitializeMenu() - .Await((e) => { NotificationHelper.ShowMessage($"初始化主菜单失败:{e.Message}"); }, + .Await((e) => { NotificationHelper.ShowError($"初始化主菜单失败:{e.Message}", e); }, () => { MessageHelper.SendLoadMessage(LoadTypes.Menu); }); Host.Services.GetRequiredService(); } catch (Exception exception) { - NotificationHelper.ShowMessage("加载数据时发生错误,如果是连接字符串不正确,可以在设置界面更改:{exception.Message}",NotificationType.Error); + NotificationHelper.ShowError("加载数据时发生错误,如果是连接字符串不正确,可以在设置界面更改:{exception.Message}", exception); } MainWindow = Host.Services.GetRequiredService(); diff --git a/Data/Repositories/DeviceRepository.cs b/Data/Repositories/DeviceRepository.cs index 749260f..4785627 100644 --- a/Data/Repositories/DeviceRepository.cs +++ b/Data/Repositories/DeviceRepository.cs @@ -201,9 +201,7 @@ public class DeviceRepository // 中间出错了 回滚 await db.RollbackTranAsync(); // 捕获并记录所有未预期的异常 - string errorMsg = $"在添加设备过程中发生未预期错误:"; - Logger.Error(errorMsg + e); - NotificationHelper.ShowMessage(errorMsg + e.Message, NotificationType.Error); + NotificationHelper.ShowError("添加设备的过程中发生了不可预期的错误:" + e.Message, e); } finally { diff --git a/Services/DataServices.cs b/Services/DataServices.cs index 68cc936..7ca9f82 100644 --- a/Services/DataServices.cs +++ b/Services/DataServices.cs @@ -127,8 +127,7 @@ public partial class DataServices : ObservableRecipient, IRecipient catch (Exception e) { // 捕获加载数据时发生的异常,并通过通知和日志记录错误信息。 - NotificationHelper.ShowMessage($"加载数据出现了错误:{e.Message}"); - _logger.LogError($"加载数据出现了错误:{e}"); + NotificationHelper.ShowError($"加载数据出现了错误:{e.Message}", e); } } diff --git a/Services/MqttBackgroundService.cs b/Services/MqttBackgroundService.cs index b6d2bcb..2a8115e 100644 --- a/Services/MqttBackgroundService.cs +++ b/Services/MqttBackgroundService.cs @@ -203,14 +203,14 @@ namespace PMSWPF.Services client.UseConnectedHandler(e => { Logger.Info($"Connected to MQTT broker: {mqtt.Name}"); - NotificationHelper.ShowMessage($"已连接到MQTT服务器: {mqtt.Name}", NotificationType.Success); + NotificationHelper.ShowSuccess($"已连接到MQTT服务器: {mqtt.Name}"); }); // 设置断开连接事件处理程序。 client.UseDisconnectedHandler(async e => { Logger.Warn($"Disconnected from MQTT broker: {mqtt.Name}. Reason: {e.Reason}"); - NotificationHelper.ShowMessage($"与MQTT服务器断开连接: {mqtt.Name}", NotificationType.Warning); + NotificationHelper.ShowInfo($"与MQTT服务器断开连接: {mqtt.Name}"); // 尝试重新连接。 await Task.Delay(TimeSpan.FromSeconds(5)); // 等待5秒后重连 try @@ -231,7 +231,7 @@ namespace PMSWPF.Services catch (Exception ex) { Logger.Error(ex, $"Failed to connect to MQTT broker: {mqtt.Name}"); - NotificationHelper.ShowMessage($"连接MQTT服务器失败: {mqtt.Name} - {ex.Message}", NotificationType.Error); + NotificationHelper.ShowError($"连接MQTT服务器失败: {mqtt.Name} - {ex.Message}", ex); } } diff --git a/Services/NavgatorServices.cs b/Services/NavgatorServices.cs index b43072f..455a85b 100644 --- a/Services/NavgatorServices.cs +++ b/Services/NavgatorServices.cs @@ -63,8 +63,7 @@ public partial class NavgatorServices : ObservableRecipient, IRecipient @@ -131,7 +131,7 @@ namespace PMSWPF.ViewModels // TODO: 实现选择变量的对话框,让用户选择要添加的变量 // 例如:var selectedVariables = await _dialogService.ShowVariableSelectionDialogAsync(); // 这里只是一个占位符,实际需要一个UI来选择变量 - NotificationHelper.ShowMessage("添加变量功能待实现,需要一个变量选择对话框。", NotificationType.Info); + NotificationHelper.ShowInfo("添加变量功能待实现,需要一个变量选择对话框。"); _logger.LogInformation("Add variables to MQTT server initiated."); // 假设我们已经通过对话框获取到了一些要添加的变量 diff --git a/ViewModels/MqttsViewModel.cs b/ViewModels/MqttsViewModel.cs index 4d6ad2d..256a671 100644 --- a/ViewModels/MqttsViewModel.cs +++ b/ViewModels/MqttsViewModel.cs @@ -81,7 +81,7 @@ public partial class MqttsViewModel : ViewModelBase { if (SelectedMqtt == null) { - NotificationHelper.ShowMessage("你没有选择任何MQTT,请选择MQTT后再点击删除", NotificationType.Error); + NotificationHelper.ShowError("你没有选择任何MQTT,请选择MQTT后再点击删除"); return; } @@ -92,7 +92,7 @@ public partial class MqttsViewModel : ViewModelBase await _mqttRepository.Delete(SelectedMqtt); MessageHelper.SendLoadMessage(LoadTypes.Mqtts); MessageHelper.SendLoadMessage(LoadTypes.Menu); - NotificationHelper.ShowMessage($"删除MQTT成功,MQTT名:{SelectedMqtt.Name}", NotificationType.Success); + NotificationHelper.ShowSuccess($"删除MQTT成功,MQTT名:{SelectedMqtt.Name}"); } } catch (Exception e) @@ -108,7 +108,7 @@ public partial class MqttsViewModel : ViewModelBase { if (SelectedMqtt == null) { - NotificationHelper.ShowMessage("你没有选择任何MQTT,请选择MQTT后再点击编辑", NotificationType.Error); + NotificationHelper.ShowError("你没有选择任何MQTT,请选择MQTT后再点击编辑"); return; } diff --git a/ViewModels/SettingViewModel.cs b/ViewModels/SettingViewModel.cs index 997d042..0314195 100644 --- a/ViewModels/SettingViewModel.cs +++ b/ViewModels/SettingViewModel.cs @@ -114,12 +114,12 @@ public partial class SettingViewModel : ViewModelBase using (var db = DbContext.GetInstance()) { await db.Ado.OpenAsync(); - NotificationHelper.ShowMessage("连接成功!"); + NotificationHelper.ShowSuccess("连接成功!"); } } catch (Exception ex) { - NotificationHelper.ShowMessage($"连接失败:{ex.Message}"); + NotificationHelper.ShowError($"连接失败:{ex.Message}", ex); } } } \ No newline at end of file diff --git a/ViewModels/VariableTableViewModel.cs b/ViewModels/VariableTableViewModel.cs index fa74ed7..af5bc0b 100644 --- a/ViewModels/VariableTableViewModel.cs +++ b/ViewModels/VariableTableViewModel.cs @@ -115,7 +115,7 @@ partial class VariableTableViewModel : ViewModelBase modifiedData.IsModified = false; } - NotificationHelper.ShowMessage($"修改的{modifiedDatas.Count}变量保存成功.", NotificationType.Success); + NotificationHelper.ShowSuccess($"修改的{modifiedDatas.Count}变量保存成功."); } [RelayCommand] @@ -136,7 +136,7 @@ partial class VariableTableViewModel : ViewModelBase // 更新变量表中的 if (index >= 0 && index < variableTable.DataVariables.Count) variableTable.DataVariables[index] = varData; - NotificationHelper.ShowMessage($"编辑变量成功:{varData?.Name}", NotificationType.Success); + NotificationHelper.ShowSuccess($"编辑变量成功:{varData?.Name}"); } catch (Exception e) { @@ -175,7 +175,7 @@ partial class VariableTableViewModel : ViewModelBase string msgSuccess = $"成功导入变量:{resVarDataCount}个。"; Logger.Info(msgSuccess); - NotificationHelper.ShowMessage(msgSuccess, NotificationType.Success); + NotificationHelper.ShowSuccess(msgSuccess); } catch (Exception e) @@ -203,7 +203,7 @@ partial class VariableTableViewModel : ViewModelBase await _varDataRepository.AddAsync(varData); // 更新当前页面的 DataVariables.Add(varData); - NotificationHelper.ShowMessage($"添加变量成功:{varData?.Name}", NotificationType.Success); + NotificationHelper.ShowSuccess($"添加变量成功:{varData?.Name}"); } catch (Exception e) { @@ -216,7 +216,7 @@ partial class VariableTableViewModel : ViewModelBase { if (variablesToDelete == null || !variablesToDelete.Any()) { - NotificationHelper.ShowMessage("请选择要删除的变量", NotificationType.Warning); + NotificationHelper.ShowInfo("请选择要删除的变量"); return; } @@ -238,11 +238,11 @@ partial class VariableTableViewModel : ViewModelBase { DataVariables.Remove(variable); } - NotificationHelper.ShowMessage($"成功删除 {result} 个变量", NotificationType.Success); + NotificationHelper.ShowSuccess($"成功删除 {result} 个变量"); } else { - NotificationHelper.ShowMessage("删除变量失败", NotificationType.Error); + NotificationHelper.ShowError("删除变量失败"); } } catch (Exception e) @@ -256,7 +256,7 @@ partial class VariableTableViewModel : ViewModelBase { if (variablesToChange == null || !variablesToChange.Any()) { - NotificationHelper.ShowMessage("请选择要修改轮询频率的变量", NotificationType.Warning); + NotificationHelper.ShowInfo("请选择要修改轮询频率的变量"); return; } @@ -270,7 +270,7 @@ partial class VariableTableViewModel : ViewModelBase } await _varDataRepository.UpdateAsync(variablesToChange); - NotificationHelper.ShowMessage($"已成功更新 {variablesToChange.Count} 个变量的轮询频率", NotificationType.Success); + NotificationHelper.ShowSuccess($"已成功更新 {variablesToChange.Count} 个变量的轮询频率"); } } @@ -279,7 +279,7 @@ partial class VariableTableViewModel : ViewModelBase { if (variablesToAddMqtt == null || !variablesToAddMqtt.Any()) { - NotificationHelper.ShowMessage("请选择要添加MQTT服务器的变量", NotificationType.Warning); + NotificationHelper.ShowInfo("请选择要添加MQTT服务器的变量"); return; } @@ -307,7 +307,7 @@ partial class VariableTableViewModel : ViewModelBase // 批量更新数据库 await _varDataRepository.UpdateAsync(variablesToAddMqtt.ToList()); - NotificationHelper.ShowMessage($"已成功为 {variablesToAddMqtt.Count} 个变量添加MQTT服务器: {selectedMqtt.Name}", NotificationType.Success); + NotificationHelper.ShowSuccess($"已成功为 {variablesToAddMqtt.Count} 个变量添加MQTT服务器: {selectedMqtt.Name}"); } catch (Exception ex) { @@ -333,11 +333,11 @@ partial class VariableTableViewModel : ViewModelBase if (res > 0) { var statusMessage = active ? "已启用" : "已停用"; - NotificationHelper.ShowMessage($"变量表:{VariableTable.Name},{statusMessage}", NotificationType.Success); + NotificationHelper.ShowSuccess($"变量表:{VariableTable.Name},{statusMessage}"); } else { - NotificationHelper.ShowMessage($"变量表:{VariableTable.Name},状态修改失败,状态:{active}", NotificationType.Error); + NotificationHelper.ShowError($"变量表:{VariableTable.Name},状态修改失败,状态:{active}"); // _logger.LogInformation($"变量表:{VariableTable.Name},状态修改失败,状态:{active}"); } } diff --git a/Views/MainView.xaml.cs b/Views/MainView.xaml.cs index 12cb8c6..98daba9 100644 --- a/Views/MainView.xaml.cs +++ b/Views/MainView.xaml.cs @@ -43,7 +43,7 @@ public partial class MainView : Window } else { - NotificationHelper.ShowMessage("选择的菜单项为空!", NotificationType.Error); + NotificationHelper.ShowError("选择的菜单项为空!"); } } diff --git a/Views/VariableTableView.xaml.cs b/Views/VariableTableView.xaml.cs index d17848d..9ff1542 100644 --- a/Views/VariableTableView.xaml.cs +++ b/Views/VariableTableView.xaml.cs @@ -40,7 +40,7 @@ public partial class VariableTableView : UserControl } catch (Exception exception) { - NotificationHelper.ShowMessage($"修改变量表启用,停用时发生了错误:{exception.Message}"); + NotificationHelper.ShowError($"修改变量表启用,停用时发生了错误:{exception.Message}", exception); } } @@ -103,9 +103,7 @@ public partial class VariableTableView : UserControl } catch (Exception e) { - string msg = "编辑变量表数据时发生了错误:"; - Logger.Error(msg + e); - NotificationHelper.ShowMessage(msg + e.Message, NotificationType.Error); + NotificationHelper.ShowError("变量表编辑的过过程中发生了错误:" + e.Message, e); } } @@ -119,7 +117,7 @@ public partial class VariableTableView : UserControl } else { - NotificationHelper.ShowMessage("请选择要删除的变量", NotificationType.Warning); + NotificationHelper.ShowInfo("请选择要删除的变量"); } } @@ -135,14 +133,12 @@ public partial class VariableTableView : UserControl } else { - NotificationHelper.ShowMessage("请选择要修改轮询频率的变量", NotificationType.Warning); + NotificationHelper.ShowInfo("请选择要修改轮询频率的变量"); } } catch (Exception e) { - string msg = "修改轮询时间时发生了错误:"; - Logger.Error(msg + e); - NotificationHelper.ShowMessage(msg + e.Message, NotificationType.Error); + NotificationHelper.ShowError("修改轮询频率的过程中发生了错误:" + e.Message, e); } } @@ -158,14 +154,12 @@ public partial class VariableTableView : UserControl } else { - NotificationHelper.ShowMessage("请选择要添加MQTT服务器的变量", NotificationType.Warning); + NotificationHelper.ShowInfo("请选择要添加MQTT服务器的变量"); } } catch (Exception ex) { - string msg = "添加MQTT服务器时发生了错误:"; - Logger.Error(msg + ex); - NotificationHelper.ShowMessage(msg + ex.Message, NotificationType.Error); + NotificationHelper.ShowError("给变量添加MQTT服务器的过程中发生了错误:" + ex.Message, ex); } } } \ No newline at end of file