将 NotificationHelper.ShowMessage改为具体的ShowError得方法

This commit is contained in:
2025-07-06 15:36:53 +08:00
parent b0874a4f02
commit 0980f84d09
13 changed files with 45 additions and 55 deletions

View File

@@ -53,13 +53,13 @@ public partial class App : Application
{ {
InitializeDataBase(); InitializeDataBase();
InitializeMenu() InitializeMenu()
.Await((e) => { NotificationHelper.ShowMessage($"初始化主菜单失败:{e.Message}"); }, .Await((e) => { NotificationHelper.ShowError($"初始化主菜单失败:{e.Message}", e); },
() => { MessageHelper.SendLoadMessage(LoadTypes.Menu); }); () => { MessageHelper.SendLoadMessage(LoadTypes.Menu); });
Host.Services.GetRequiredService<GrowlNotificationService>(); Host.Services.GetRequiredService<GrowlNotificationService>();
} }
catch (Exception exception) catch (Exception exception)
{ {
NotificationHelper.ShowMessage("加载数据时发生错误,如果是连接字符串不正确,可以在设置界面更改:{exception.Message}",NotificationType.Error); NotificationHelper.ShowError("加载数据时发生错误,如果是连接字符串不正确,可以在设置界面更改:{exception.Message}", exception);
} }
MainWindow = Host.Services.GetRequiredService<MainView>(); MainWindow = Host.Services.GetRequiredService<MainView>();

View File

@@ -201,9 +201,7 @@ public class DeviceRepository
// 中间出错了 回滚 // 中间出错了 回滚
await db.RollbackTranAsync(); await db.RollbackTranAsync();
// 捕获并记录所有未预期的异常 // 捕获并记录所有未预期的异常
string errorMsg = $"在添加设备过程中发生预期错误:"; NotificationHelper.ShowError("添加设备过程中发生了不可预期错误:" + e.Message, e);
Logger.Error(errorMsg + e);
NotificationHelper.ShowMessage(errorMsg + e.Message, NotificationType.Error);
} }
finally finally
{ {

View File

@@ -127,8 +127,7 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
catch (Exception e) catch (Exception e)
{ {
// 捕获加载数据时发生的异常,并通过通知和日志记录错误信息。 // 捕获加载数据时发生的异常,并通过通知和日志记录错误信息。
NotificationHelper.ShowMessage($"加载数据出现了错误:{e.Message}"); NotificationHelper.ShowError($"加载数据出现了错误:{e.Message}", e);
_logger.LogError($"加载数据出现了错误:{e}");
} }
} }

View File

@@ -203,14 +203,14 @@ namespace PMSWPF.Services
client.UseConnectedHandler(e => client.UseConnectedHandler(e =>
{ {
Logger.Info($"Connected to MQTT broker: {mqtt.Name}"); Logger.Info($"Connected to MQTT broker: {mqtt.Name}");
NotificationHelper.ShowMessage($"已连接到MQTT服务器: {mqtt.Name}", NotificationType.Success); NotificationHelper.ShowSuccess($"已连接到MQTT服务器: {mqtt.Name}");
}); });
// 设置断开连接事件处理程序。 // 设置断开连接事件处理程序。
client.UseDisconnectedHandler(async e => client.UseDisconnectedHandler(async e =>
{ {
Logger.Warn($"Disconnected from MQTT broker: {mqtt.Name}. Reason: {e.Reason}"); 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秒后重连 await Task.Delay(TimeSpan.FromSeconds(5)); // 等待5秒后重连
try try
@@ -231,7 +231,7 @@ namespace PMSWPF.Services
catch (Exception ex) catch (Exception ex)
{ {
Logger.Error(ex, $"Failed to connect to MQTT broker: {mqtt.Name}"); 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);
} }
} }

View File

@@ -63,8 +63,7 @@ public partial class NavgatorServices : ObservableRecipient, IRecipient<Navgator
} }
catch (Exception e) catch (Exception e)
{ {
NotificationHelper.ShowMessage($"切换视图时发生了错误:{e.Message}", NotificationType.Error); NotificationHelper.ShowError($"切换视图时发生了错误:{e.Message}", e);
_logger.LogError($"切换视图时发生了错误:{e}");
} }
} }

View File

@@ -92,7 +92,7 @@ public partial class DevicesViewModel : ViewModelBase
{ {
if (SelectedDevice == null) if (SelectedDevice == null)
{ {
NotificationHelper.ShowMessage("你没有选择任何设备,请选择设备后再点击删除设备", NotificationType.Error); NotificationHelper.ShowError("你没有选择任何设备,请选择设备后再点击删除设备");
return; return;
} }
@@ -106,7 +106,7 @@ public partial class DevicesViewModel : ViewModelBase
MessageHelper.SendLoadMessage(LoadTypes.Menu); MessageHelper.SendLoadMessage(LoadTypes.Menu);
MessageHelper.SendLoadMessage(LoadTypes.Devices); MessageHelper.SendLoadMessage(LoadTypes.Devices);
NotificationHelper.ShowMessage($"删除设备成功,设备名:{SelectedDevice.Name}", NotificationType.Success); NotificationHelper.ShowSuccess($"删除设备成功,设备名:{SelectedDevice.Name}");
} }
} }
catch (Exception e) catch (Exception e)
@@ -125,7 +125,7 @@ public partial class DevicesViewModel : ViewModelBase
{ {
if (SelectedDevice == null) if (SelectedDevice == null)
{ {
NotificationHelper.ShowMessage("你没有选择任何设备,请选择设备后再点击编辑设备", NotificationType.Error); NotificationHelper.ShowError("你没有选择任何设备,请选择设备后再点击编辑设备");
return; return;
} }

View File

@@ -77,7 +77,7 @@ public partial class MainViewModel : ViewModelBase
if (menu.Parent?.Data is not Device device) if (menu.Parent?.Data is not Device device)
{ {
_logger.LogWarning("尝试添加变量表时Parent 或 Parent.Data 为空,或 Parent.Data 不是 Device 类型。"); _logger.LogWarning("尝试添加变量表时Parent 或 Parent.Data 为空,或 Parent.Data 不是 Device 类型。");
NotificationHelper.ShowMessage("操作失败:无法获取有效的设备信息。", NotificationType.Error); NotificationHelper.ShowError("操作失败:无法获取有效的设备信息。");
return; return;
} }
@@ -118,14 +118,14 @@ public partial class MainViewModel : ViewModelBase
// 变量表和菜单都添加成功 // 变量表和菜单都添加成功
MessageHelper.SendLoadMessage(LoadTypes.Menu); MessageHelper.SendLoadMessage(LoadTypes.Menu);
MessageHelper.SendLoadMessage(LoadTypes.Devices); MessageHelper.SendLoadMessage(LoadTypes.Devices);
NotificationHelper.ShowMessage($"变量表:{varTable.Name},添加成功", NotificationType.Success); NotificationHelper.ShowSuccess($"变量表:{varTable.Name},添加成功");
_logger.LogInformation($"变量表:{varTable.Name},添加成功"); _logger.LogInformation($"变量表:{varTable.Name},添加成功");
} }
else else
{ {
await db.RollbackTranAsync(); await db.RollbackTranAsync();
// 变量表菜单添加失败 (此时变量表可能已添加成功,需要根据业务决定是否回滚) // 变量表菜单添加失败 (此时变量表可能已添加成功,需要根据业务决定是否回滚)
NotificationHelper.ShowMessage($"变量表:{varTable.Name},添加菜单失败", NotificationType.Error); NotificationHelper.ShowError($"变量表:{varTable.Name},添加菜单失败");
_logger.LogError($"变量表:{varTable.Name},添加菜单失败"); _logger.LogError($"变量表:{varTable.Name},添加菜单失败");
// 考虑:如果菜单添加失败,是否需要删除之前添加的变量表? // 考虑:如果菜单添加失败,是否需要删除之前添加的变量表?
// 例如await _varTableRepository.Delete(addVarTableId); // 例如await _varTableRepository.Delete(addVarTableId);
@@ -190,7 +190,7 @@ public partial class MainViewModel : ViewModelBase
} }
else else
{ {
NotificationHelper.ShowMessage($"菜单:{menu.Name},没有对应的ViewModel."); NotificationHelper.ShowInfo($"菜单:{menu.Name},没有对应的ViewModel.");
_logger.LogInformation($"菜单:{menu.Name},没有对应的ViewModel."); _logger.LogInformation($"菜单:{menu.Name},没有对应的ViewModel.");
} }
} }

View File

@@ -81,7 +81,7 @@ namespace PMSWPF.ViewModels
// await _dataServices.UpdateVariableDataAsync(variable); // await _dataServices.UpdateVariableDataAsync(variable);
// } // }
NotificationHelper.ShowMessage("MQTT服务器详情保存功能待实现。", NotificationType.Info); NotificationHelper.ShowInfo("MQTT服务器详情保存功能待实现。");
_logger.LogInformation("Save changes for MQTT server detail initiated."); _logger.LogInformation("Save changes for MQTT server detail initiated.");
} }
@@ -94,7 +94,7 @@ namespace PMSWPF.ViewModels
{ {
if (CurrentMqtt == null || variablesToRemove == null || variablesToRemove.Count == 0) if (CurrentMqtt == null || variablesToRemove == null || variablesToRemove.Count == 0)
{ {
NotificationHelper.ShowMessage("请选择要移除的变量。", NotificationType.Warning); NotificationHelper.ShowInfo("请选择要移除的变量。");
return; return;
} }
@@ -117,7 +117,7 @@ namespace PMSWPF.ViewModels
// TODO: 这里需要调用DataServices来更新数据库中VariableData的Mqtt关联 // TODO: 这里需要调用DataServices来更新数据库中VariableData的Mqtt关联
// 例如await _dataServices.UpdateVariableDataAssociationsAsync(variablesToRemove); // 例如await _dataServices.UpdateVariableDataAssociationsAsync(variablesToRemove);
NotificationHelper.ShowMessage("变量移除成功,请记得保存更改。", NotificationType.Success); NotificationHelper.ShowSuccess("变量移除成功,请记得保存更改。");
} }
/// <summary> /// <summary>
@@ -131,7 +131,7 @@ namespace PMSWPF.ViewModels
// TODO: 实现选择变量的对话框,让用户选择要添加的变量 // TODO: 实现选择变量的对话框,让用户选择要添加的变量
// 例如var selectedVariables = await _dialogService.ShowVariableSelectionDialogAsync(); // 例如var selectedVariables = await _dialogService.ShowVariableSelectionDialogAsync();
// 这里只是一个占位符实际需要一个UI来选择变量 // 这里只是一个占位符实际需要一个UI来选择变量
NotificationHelper.ShowMessage("添加变量功能待实现,需要一个变量选择对话框。", NotificationType.Info); NotificationHelper.ShowInfo("添加变量功能待实现,需要一个变量选择对话框。");
_logger.LogInformation("Add variables to MQTT server initiated."); _logger.LogInformation("Add variables to MQTT server initiated.");
// 假设我们已经通过对话框获取到了一些要添加的变量 // 假设我们已经通过对话框获取到了一些要添加的变量

View File

@@ -81,7 +81,7 @@ public partial class MqttsViewModel : ViewModelBase
{ {
if (SelectedMqtt == null) if (SelectedMqtt == null)
{ {
NotificationHelper.ShowMessage("你没有选择任何MQTT请选择MQTT后再点击删除", NotificationType.Error); NotificationHelper.ShowError("你没有选择任何MQTT请选择MQTT后再点击删除");
return; return;
} }
@@ -92,7 +92,7 @@ public partial class MqttsViewModel : ViewModelBase
await _mqttRepository.Delete(SelectedMqtt); await _mqttRepository.Delete(SelectedMqtt);
MessageHelper.SendLoadMessage(LoadTypes.Mqtts); MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
MessageHelper.SendLoadMessage(LoadTypes.Menu); MessageHelper.SendLoadMessage(LoadTypes.Menu);
NotificationHelper.ShowMessage($"删除MQTT成功,MQTT名{SelectedMqtt.Name}", NotificationType.Success); NotificationHelper.ShowSuccess($"删除MQTT成功,MQTT名{SelectedMqtt.Name}");
} }
} }
catch (Exception e) catch (Exception e)
@@ -108,7 +108,7 @@ public partial class MqttsViewModel : ViewModelBase
{ {
if (SelectedMqtt == null) if (SelectedMqtt == null)
{ {
NotificationHelper.ShowMessage("你没有选择任何MQTT请选择MQTT后再点击编辑", NotificationType.Error); NotificationHelper.ShowError("你没有选择任何MQTT请选择MQTT后再点击编辑");
return; return;
} }

View File

@@ -114,12 +114,12 @@ public partial class SettingViewModel : ViewModelBase
using (var db = DbContext.GetInstance()) using (var db = DbContext.GetInstance())
{ {
await db.Ado.OpenAsync(); await db.Ado.OpenAsync();
NotificationHelper.ShowMessage("连接成功!"); NotificationHelper.ShowSuccess("连接成功!");
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
NotificationHelper.ShowMessage($"连接失败:{ex.Message}"); NotificationHelper.ShowError($"连接失败:{ex.Message}", ex);
} }
} }
} }

View File

@@ -115,7 +115,7 @@ partial class VariableTableViewModel : ViewModelBase
modifiedData.IsModified = false; modifiedData.IsModified = false;
} }
NotificationHelper.ShowMessage($"修改的{modifiedDatas.Count}变量保存成功.", NotificationType.Success); NotificationHelper.ShowSuccess($"修改的{modifiedDatas.Count}变量保存成功.");
} }
[RelayCommand] [RelayCommand]
@@ -136,7 +136,7 @@ partial class VariableTableViewModel : ViewModelBase
// 更新变量表中的 // 更新变量表中的
if (index >= 0 && index < variableTable.DataVariables.Count) if (index >= 0 && index < variableTable.DataVariables.Count)
variableTable.DataVariables[index] = varData; variableTable.DataVariables[index] = varData;
NotificationHelper.ShowMessage($"编辑变量成功:{varData?.Name}", NotificationType.Success); NotificationHelper.ShowSuccess($"编辑变量成功:{varData?.Name}");
} }
catch (Exception e) catch (Exception e)
{ {
@@ -175,7 +175,7 @@ partial class VariableTableViewModel : ViewModelBase
string msgSuccess = $"成功导入变量:{resVarDataCount}个。"; string msgSuccess = $"成功导入变量:{resVarDataCount}个。";
Logger.Info(msgSuccess); Logger.Info(msgSuccess);
NotificationHelper.ShowMessage(msgSuccess, NotificationType.Success); NotificationHelper.ShowSuccess(msgSuccess);
} }
catch (Exception e) catch (Exception e)
@@ -203,7 +203,7 @@ partial class VariableTableViewModel : ViewModelBase
await _varDataRepository.AddAsync(varData); await _varDataRepository.AddAsync(varData);
// 更新当前页面的 // 更新当前页面的
DataVariables.Add(varData); DataVariables.Add(varData);
NotificationHelper.ShowMessage($"添加变量成功:{varData?.Name}", NotificationType.Success); NotificationHelper.ShowSuccess($"添加变量成功:{varData?.Name}");
} }
catch (Exception e) catch (Exception e)
{ {
@@ -216,7 +216,7 @@ partial class VariableTableViewModel : ViewModelBase
{ {
if (variablesToDelete == null || !variablesToDelete.Any()) if (variablesToDelete == null || !variablesToDelete.Any())
{ {
NotificationHelper.ShowMessage("请选择要删除的变量", NotificationType.Warning); NotificationHelper.ShowInfo("请选择要删除的变量");
return; return;
} }
@@ -238,11 +238,11 @@ partial class VariableTableViewModel : ViewModelBase
{ {
DataVariables.Remove(variable); DataVariables.Remove(variable);
} }
NotificationHelper.ShowMessage($"成功删除 {result} 个变量", NotificationType.Success); NotificationHelper.ShowSuccess($"成功删除 {result} 个变量");
} }
else else
{ {
NotificationHelper.ShowMessage("删除变量失败", NotificationType.Error); NotificationHelper.ShowError("删除变量失败");
} }
} }
catch (Exception e) catch (Exception e)
@@ -256,7 +256,7 @@ partial class VariableTableViewModel : ViewModelBase
{ {
if (variablesToChange == null || !variablesToChange.Any()) if (variablesToChange == null || !variablesToChange.Any())
{ {
NotificationHelper.ShowMessage("请选择要修改轮询频率的变量", NotificationType.Warning); NotificationHelper.ShowInfo("请选择要修改轮询频率的变量");
return; return;
} }
@@ -270,7 +270,7 @@ partial class VariableTableViewModel : ViewModelBase
} }
await _varDataRepository.UpdateAsync(variablesToChange); 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()) if (variablesToAddMqtt == null || !variablesToAddMqtt.Any())
{ {
NotificationHelper.ShowMessage("请选择要添加MQTT服务器的变量", NotificationType.Warning); NotificationHelper.ShowInfo("请选择要添加MQTT服务器的变量");
return; return;
} }
@@ -307,7 +307,7 @@ partial class VariableTableViewModel : ViewModelBase
// 批量更新数据库 // 批量更新数据库
await _varDataRepository.UpdateAsync(variablesToAddMqtt.ToList()); await _varDataRepository.UpdateAsync(variablesToAddMqtt.ToList());
NotificationHelper.ShowMessage($"已成功为 {variablesToAddMqtt.Count} 个变量添加MQTT服务器: {selectedMqtt.Name}", NotificationType.Success); NotificationHelper.ShowSuccess($"已成功为 {variablesToAddMqtt.Count} 个变量添加MQTT服务器: {selectedMqtt.Name}");
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -333,11 +333,11 @@ partial class VariableTableViewModel : ViewModelBase
if (res > 0) if (res > 0)
{ {
var statusMessage = active ? "已启用" : "已停用"; var statusMessage = active ? "已启用" : "已停用";
NotificationHelper.ShowMessage($"变量表:{VariableTable.Name},{statusMessage}", NotificationType.Success); NotificationHelper.ShowSuccess($"变量表:{VariableTable.Name},{statusMessage}");
} }
else else
{ {
NotificationHelper.ShowMessage($"变量表:{VariableTable.Name},状态修改失败,状态:{active}", NotificationType.Error); NotificationHelper.ShowError($"变量表:{VariableTable.Name},状态修改失败,状态:{active}");
// _logger.LogInformation($"变量表:{VariableTable.Name},状态修改失败,状态:{active}"); // _logger.LogInformation($"变量表:{VariableTable.Name},状态修改失败,状态:{active}");
} }
} }

View File

@@ -43,7 +43,7 @@ public partial class MainView : Window
} }
else else
{ {
NotificationHelper.ShowMessage("选择的菜单项为空!", NotificationType.Error); NotificationHelper.ShowError("选择的菜单项为空!");
} }
} }

View File

@@ -40,7 +40,7 @@ public partial class VariableTableView : UserControl
} }
catch (Exception exception) catch (Exception exception)
{ {
NotificationHelper.ShowMessage($"修改变量表启用,停用时发生了错误:{exception.Message}"); NotificationHelper.ShowError($"修改变量表启用,停用时发生了错误:{exception.Message}", exception);
} }
} }
@@ -103,9 +103,7 @@ public partial class VariableTableView : UserControl
} }
catch (Exception e) catch (Exception e)
{ {
string msg = "编辑变量表数据时发生了错误:"; NotificationHelper.ShowError("变量表编辑的过过程中发生了错误:" + e.Message, e);
Logger.Error(msg + e);
NotificationHelper.ShowMessage(msg + e.Message, NotificationType.Error);
} }
} }
@@ -119,7 +117,7 @@ public partial class VariableTableView : UserControl
} }
else else
{ {
NotificationHelper.ShowMessage("请选择要删除的变量", NotificationType.Warning); NotificationHelper.ShowInfo("请选择要删除的变量");
} }
} }
@@ -135,14 +133,12 @@ public partial class VariableTableView : UserControl
} }
else else
{ {
NotificationHelper.ShowMessage("请选择要修改轮询频率的变量", NotificationType.Warning); NotificationHelper.ShowInfo("请选择要修改轮询频率的变量");
} }
} }
catch (Exception e) catch (Exception e)
{ {
string msg = "修改轮询时间时发生了错误:"; NotificationHelper.ShowError("修改轮询频率的过程中发生了错误:" + e.Message, e);
Logger.Error(msg + e);
NotificationHelper.ShowMessage(msg + e.Message, NotificationType.Error);
} }
} }
@@ -158,14 +154,12 @@ public partial class VariableTableView : UserControl
} }
else else
{ {
NotificationHelper.ShowMessage("请选择要添加MQTT服务器的变量", NotificationType.Warning); NotificationHelper.ShowInfo("请选择要添加MQTT服务器的变量");
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
string msg = "添加MQTT服务器发生了错误:"; NotificationHelper.ShowError("给变量添加MQTT服务器的过程中发生了错误:" + ex.Message, ex);
Logger.Error(msg + ex);
NotificationHelper.ShowMessage(msg + ex.Message, NotificationType.Error);
} }
} }
} }