From 5ce3825fa94919c737387c4434480640aea8ef0e Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Tue, 2 Sep 2025 18:29:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BF=AE=E6=94=B9=E9=80=89?= =?UTF-8?q?=E5=AE=9A=E5=8F=98=E9=87=8F=E7=9A=84=E5=90=AF=E7=94=A8=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DMS.WPF/Services/DialogService.cs | 1 + .../Dialogs/IsActiveDialogViewModel.cs | 42 +++++------ DMS.WPF/ViewModels/VariableTableViewModel.cs | 75 ++++++++++--------- DMS.WPF/Views/Dialogs/IsActiveDialog.xaml | 47 +++++++----- DMS.WPF/Views/Dialogs/IsActiveDialog.xaml.cs | 15 ++-- DMS.WPF/Views/VariableTableView.xaml | 5 +- 6 files changed, 96 insertions(+), 89 deletions(-) diff --git a/DMS.WPF/Services/DialogService.cs b/DMS.WPF/Services/DialogService.cs index 0413fd9..bc4adb5 100644 --- a/DMS.WPF/Services/DialogService.cs +++ b/DMS.WPF/Services/DialogService.cs @@ -21,6 +21,7 @@ namespace DMS.WPF.Services { typeof(ImportOpcUaDialogViewModel), typeof(ImportOpcUaDialog) }, { typeof(VariableDialogViewModel), typeof(VariableDialog) }, { typeof(PollLevelDialogViewModel), typeof(PollLevelDialog) }, + { typeof(IsActiveDialogViewModel), typeof(IsActiveDialog) }, // { typeof(MqttDialogViewModel), typeof(MqttDialog) }, // Add other mappings here // ... other dialogs }; diff --git a/DMS.WPF/ViewModels/Dialogs/IsActiveDialogViewModel.cs b/DMS.WPF/ViewModels/Dialogs/IsActiveDialogViewModel.cs index ef91229..5ba216e 100644 --- a/DMS.WPF/ViewModels/Dialogs/IsActiveDialogViewModel.cs +++ b/DMS.WPF/ViewModels/Dialogs/IsActiveDialogViewModel.cs @@ -1,31 +1,31 @@ +using System; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; -using DMS.Core.Enums; -namespace DMS.WPF.ViewModels.Dialogs; - -public partial class IsActiveDialogViewModel : ObservableObject +namespace DMS.WPF.ViewModels.Dialogs { - [ObservableProperty] - private bool? _selectedIsActive; - - public IsActiveDialogViewModel(bool? currentIsActive) + public partial class IsActiveDialogViewModel : DialogViewModelBase { - _selectedIsActive = currentIsActive; - } + [ObservableProperty] + private bool? _selectedIsActive; - [RelayCommand] - private void SelectIsActive(string isActiveString) - { - if (bool.TryParse(isActiveString, out bool isActive)) + public IsActiveDialogViewModel(bool currentIsActive) { - SelectedIsActive = isActive; + SelectedIsActive = currentIsActive; + Title = "修改启用状态"; + PrimaryButText = "确定"; + } + + [RelayCommand] + private void PrimaryButton() + { + Close(SelectedIsActive); + } + + [RelayCommand] + private void CancleButton() + { + Close(null); } } - - [RelayCommand] - private void Cancel() - { - SelectedIsActive = null; - } } \ No newline at end of file diff --git a/DMS.WPF/ViewModels/VariableTableViewModel.cs b/DMS.WPF/ViewModels/VariableTableViewModel.cs index cddd786..4aed29c 100644 --- a/DMS.WPF/ViewModels/VariableTableViewModel.cs +++ b/DMS.WPF/ViewModels/VariableTableViewModel.cs @@ -12,6 +12,8 @@ using DMS.WPF.ViewModels.Items; using Microsoft.Extensions.DependencyInjection; using ObservableCollections; using System.Collections; +using System.Collections.Generic; +using System.Linq; namespace DMS.WPF.ViewModels; @@ -638,43 +640,44 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable /// /// 要修改启用状态的变量数据列表。 [RelayCommand] - public async Task ModifyIsActive(IList variablesToChange) + public async Task ChangeIsActive() { - // var validVariables = variablesToChange?.OfType() - // .ToList(); - // - // if (validVariables == null || !validVariables.Any()) - // { - // NotificationHelper.ShowInfo("请选择要修改启用状态的变量"); - // return; - // } - // - // // 假设所有选中的变量都应该被设置为相同的状态,取第一个变量的当前状态的反值 - // var currentIsActive = validVariables.First() - // .IsActive; - // var newIsActive = !currentIsActive; - // - // var confirm = await _dialogService.ShowIsActiveDialog(newIsActive); - // - // if (confirm.HasValue && confirm.Value == newIsActive) - // { - // foreach (var variable in validVariables) - // { - // variable.IsActive = newIsActive; - // } - // - // await _varDataRepository.UpdateAsync(validVariables); - // - // // 更新界面 - // await RefreshDataView(); - // - // - // NotificationHelper.ShowSuccess($"已成功将 {validVariables.Count} 个变量的启用状态修改为 {newIsActive}"); - // } - // else - // { - // NotificationHelper.ShowInfo("操作已取消或状态未改变。"); - // } + // 检查是否有变量被选中 + if (SelectedVariables.Count == 0) + { + NotificationHelper.ShowInfo("请选择要修改启用状态的变量"); + return; + } + + // 获取选中的变量列表 + var validVariables = SelectedVariables.Cast().ToList(); + + // 显示启用状态选择对话框,并传入第一个变量的当前启用状态作为默认值 + IsActiveDialogViewModel viewModel = new IsActiveDialogViewModel(validVariables.First().IsActive); + var newIsActive = await _dialogService.ShowDialogAsync(viewModel); + if (newIsActive.HasValue) + { + // 更新所有选定变量的启用状态和修改状态 + foreach (var variable in validVariables) + { + variable.IsActive = newIsActive.Value; + variable.UpdatedAt = DateTime.Now; + } + + // 批量更新数据库中的变量数据 + var variableDtos = _mapper.Map>(validVariables); + var result = await _variableAppService.UpdateVariablesAsync(variableDtos); + + if (result > 0) + { + // 显示成功通知 + NotificationHelper.ShowSuccess($"已成功更新 {validVariables.Count} 个变量的启用状态"); + } + else + { + NotificationHelper.ShowError("更新启用状态失败"); + } + } } /// diff --git a/DMS.WPF/Views/Dialogs/IsActiveDialog.xaml b/DMS.WPF/Views/Dialogs/IsActiveDialog.xaml index 9dffdff..b4d6df4 100644 --- a/DMS.WPF/Views/Dialogs/IsActiveDialog.xaml +++ b/DMS.WPF/Views/Dialogs/IsActiveDialog.xaml @@ -1,28 +1,37 @@ - + - + - - + + - + \ No newline at end of file diff --git a/DMS.WPF/Views/Dialogs/IsActiveDialog.xaml.cs b/DMS.WPF/Views/Dialogs/IsActiveDialog.xaml.cs index ae6229b..f73d149 100644 --- a/DMS.WPF/Views/Dialogs/IsActiveDialog.xaml.cs +++ b/DMS.WPF/Views/Dialogs/IsActiveDialog.xaml.cs @@ -5,28 +5,25 @@ namespace DMS.WPF.Views.Dialogs; public partial class IsActiveDialog : ContentDialog { - public IsActiveDialogViewModel ViewModel { get; } - public IsActiveDialog(IsActiveDialogViewModel viewModel) + public IsActiveDialog() { InitializeComponent(); - ViewModel = viewModel; - DataContext = ViewModel; } private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args) { // 确认按钮点击时,ViewModel.SelectedIsActive 已经通过绑定更新 // 如果用户没有选择任何选项,可以阻止关闭对话框 - if (!ViewModel.SelectedIsActive.HasValue) - { - args.Cancel = true; - } + //if (!ViewModel.SelectedIsActive.HasValue) + //{ + // args.Cancel = true; + //} } private void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args) { // 取消按钮点击时,将 SelectedIsActive 设置为 null - ViewModel.SelectedIsActive = null; + //ViewModel.SelectedIsActive = null; } } \ No newline at end of file diff --git a/DMS.WPF/Views/VariableTableView.xaml b/DMS.WPF/Views/VariableTableView.xaml index 6132f99..50b7e06 100644 --- a/DMS.WPF/Views/VariableTableView.xaml +++ b/DMS.WPF/Views/VariableTableView.xaml @@ -196,10 +196,7 @@ - +