1 feat: 改进MQTT服务器详情页面UI和功能

2
   3 - 将MQTT服务器详情页面的文本框改为只读模式,使用TextBlock显示
   4 - 为MQTT服务器详情区域添加图标和优化间距,提升视觉效果
   5 - 实现顶部导航栏和CommandBar功能,支持返回列表、刷新等操作
   6 - 添加Reload和NavigateToMqtts命令,增强页面功能
   7 - 重构ViewModel,添加MQTT管理、数据存储和导航服务依赖
   8 - 实现页面导航参数处理,根据ID加载对应的MQTT服务器信息
   9 - 使用DockPanel和ScrollViewer优化页面布局,提升用户体验
This commit is contained in:
2025-10-05 17:50:41 +08:00
parent 6b55211dbf
commit 7a2b67991c
2 changed files with 327 additions and 180 deletions

View File

@@ -1,13 +1,11 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DMS.Application.Interfaces.Management;
using DMS.Core.Models;
using DMS.WPF.Interfaces;
using DMS.WPF.ViewModels.Items;
using Microsoft.Extensions.Logging;
using System.Collections.ObjectModel;
using DMS.Core.Models;
using DMS.Services;
using DMS.WPF.Interfaces;
using DMS.WPF.Services;
using DMS.WPF.ViewModels;
using DMS.WPF.ViewModels.Items;
namespace DMS.WPF.ViewModels
{
@@ -20,6 +18,9 @@ namespace DMS.WPF.ViewModels
private readonly ILogger<MqttServerDetailViewModel> _logger;
private readonly IDialogService _dialogService;
private readonly INotificationService _notificationService;
private readonly IMqttManagementService _mqttManagementService;
private readonly IDataStorageService _dataStorageService;
private readonly INavigationService _navigationService;
/// <summary>
/// 当前正在编辑的MQTT服务器对象。
@@ -41,112 +42,74 @@ namespace DMS.WPF.ViewModels
/// <param name="dataServices">数据服务。</param>
/// <param name="dialogService">对话框服务。</param>
/// <param name="notificationService">通知服务。</param>
/// <param name="mqttManagementService">MQTT管理服务</param>
/// <param name="navigationService">导航服务</param>
public MqttServerDetailViewModel(ILogger<MqttServerDetailViewModel> logger,
IDialogService dialogService, INotificationService notificationService)
IDialogService dialogService,
INotificationService notificationService,
IMqttManagementService mqttManagementService,
IDataStorageService dataStorageService,
INavigationService navigationService)
{
_logger = logger;
_dialogService = dialogService;
_notificationService = notificationService;
_mqttManagementService = mqttManagementService;
this._dataStorageService = dataStorageService;
_navigationService = navigationService;
}
public override void OnLoaded()
{
// if (CurrentMqtt.VariableMqtts != null)
// {
// AssociatedVariables =new ObservableCollection<VariableMqtt>(CurrentMqtt.VariableMqtts) ;
// }
}
/// <summary>
/// 保存MQTT服务器及其关联变量的更改。
/// 重新加载当前MQTT服务器数据
/// </summary>
[RelayCommand]
private async Task SaveChanges()
private async Task Reload()
{
if (CurrentMqtt == null) return;
// TODO: 实现保存逻辑。这可能涉及到更新Mqtt对象和更新VariableData对象。
// 由于Mqtt和VariableData之间的关联可能在数据库中通过中间表维护
// 这里需要根据实际的数据库操作来调整。
// 例如如果Mqtt对象本身包含关联的VariableData列表则直接保存Mqtt对象即可。
// 如果是多对多关系,可能需要更新中间表。
// 示例假设Mqtt对象需要更新
// await _dataServices.UpdateMqttAsync(CurrentMqtt);
// 示例:假设变量数据也需要保存
// foreach (var variable in AssociatedVariables.Where(v => v.IsModified))
// {
// await _dataServices.UpdateVariableAsync(variable);
// }
_notificationService.ShowInfo("MQTT服务器详情保存功能待实现。");
_logger.LogInformation("Save changes for MQTT server detail initiated.");
if (CurrentMqtt?.Id > 0)
{
// 重新加载当前MQTT服务器数据
var updatedMqtt = await _mqttManagementService.GetMqttServerByIdAsync(CurrentMqtt.Id);
if (updatedMqtt != null)
{
// 更新CurrentMqtt的属性
CurrentMqtt.ServerName = updatedMqtt.ServerName;
CurrentMqtt.ServerUrl = updatedMqtt.ServerUrl;
CurrentMqtt.Port = updatedMqtt.Port;
CurrentMqtt.ClientId = updatedMqtt.ClientId;
CurrentMqtt.Username = updatedMqtt.Username;
CurrentMqtt.Password = updatedMqtt.Password;
CurrentMqtt.PublishTopic = updatedMqtt.PublishTopic;
CurrentMqtt.SubscribeTopic = updatedMqtt.SubscribeTopic;
CurrentMqtt.MessageHeader = updatedMqtt.MessageHeader;
CurrentMqtt.MessageContent = updatedMqtt.MessageContent;
CurrentMqtt.MessageFooter = updatedMqtt.MessageFooter;
}
}
}
/// <summary>
/// 从当前MQTT服务器中移除选定的变量。
/// 导航回MQTT服务器列表页面
/// </summary>
/// <param name="variablesToRemove">要移除的变量列表。</param>
[RelayCommand]
private async Task RemoveVariables(System.Collections.IList variablesToRemove)
private async Task NavigateToMqtts()
{
// if (CurrentMqtt == null || variablesToRemove == null || variablesToRemove.Count == 0)
// {
// NotificationHelper.ShowInfo("请选择要移除的变量。");
// return;
// }
//
// var variablesList = variablesToRemove.Cast<Variable>()
// .ToList();
//
// var result = await _dialogService.ShowConfrimeDialog(
// "确认移除", $"确定要从MQTT服务器 '{CurrentMqtt.ServerName}' 中移除选定的 {variablesList.Count} 个变量吗?");
// if (result != true) return;
//
// foreach (var variable in variablesList) // 使用ToList()避免在迭代时修改集合
// {
// // 移除变量与当前MQTT服务器的关联
// // variable.Mqtts?.Remove(CurrentMqtt);
// // // 标记变量为已修改,以便保存时更新数据库
// // variable.IsModified = true;
// // AssociatedVariables.Remove(variable);
// // _logger.LogInformation($"Removed variable {variable.Name} from MQTT server {CurrentMqtt.Name}.");
// }
//
// //
// // 例如await _dataServices.UpdateVariableDataAssociationsAsync(variablesToRemove);
// NotificationHelper.ShowSuccess("变量移除成功,请记得保存更改。");
await _navigationService.NavigateToAsync(this, new NavigationParameter(nameof(MqttsViewModel)));
}
/// <summary>
/// 添加变量到当前MQTT服务器。
/// </summary>
[RelayCommand]
private async Task AddVariables()
public override Task OnNavigatedToAsync(NavigationParameter parameter)
{
if (CurrentMqtt == null) return;
if (parameter == null) return Task.CompletedTask;
// TODO: 实现选择变量的对话框,让用户选择要添加的变量
// 例如var selectedVariables = await _dialogService.ShowVariableSelectionDialogAsync();
// 这里只是一个占位符实际需要一个UI来选择变量
_notificationService.ShowInfo("添加变量功能待实现,需要一个变量选择对话框。");
_logger.LogInformation("AddAsync variables to MQTT server initiated.");
if (_dataStorageService.MqttServers.TryGetValue(parameter.TargetId, out var mqttServerItem))
{
CurrentMqtt = mqttServerItem;
// 假设我们已经通过对话框获取到了一些要添加的变量
// List<Variable> newVariables = ...;
// foreach (var variable in newVariables)
// {
// if (variable.Mqtts == null) variable.Mqtts = new List<Mqtt>();
// if (!variable.Mqtts.Any(m => m.Id == CurrentMqtt.Id))
// {
// variable.Mqtts.AddAsync(CurrentMqtt);
// variable.IsModified = true; // 标记为已修改
// AssociatedVariables.AddAsync(variable);
// }
// }
// NotificationHelper.ShowMessage("变量添加成功,请记得保存更改。", NotificationType.Success);
}
return Task.CompletedTask;
}
}
}