From 3d92737df80b6f20933b8bf713fa7d88dbde7db8 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Fri, 3 Oct 2025 10:49:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20DevicesView=20=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=20DeviceItemTemplate=20=E5=8F=98=E9=87=8F=E8=A1=A8?= =?UTF-8?q?=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DMS.WPF/ViewModels/DevicesViewModel.cs | 134 +++++++++++++++++++++++++ DMS.WPF/Views/DevicesView.xaml | 6 ++ 2 files changed, 140 insertions(+) diff --git a/DMS.WPF/ViewModels/DevicesViewModel.cs b/DMS.WPF/ViewModels/DevicesViewModel.cs index dcb484d..faac014 100644 --- a/DMS.WPF/ViewModels/DevicesViewModel.cs +++ b/DMS.WPF/ViewModels/DevicesViewModel.cs @@ -252,6 +252,140 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable { } + [RelayCommand] + private async Task AddVariableTable(DeviceItemViewModel device) + { + if (device == null) return; + + try + { + VariableTableDialogViewModel variableTableDialogViewModel = new VariableTableDialogViewModel() + { + PrimaryButText = "添加变量表" + }; + // 显示添加变量表对话框 + var variableTableItemViewModel = await _dialogService.ShowDialogAsync(variableTableDialogViewModel); + // 如果用户取消或对话框未返回变量表,则直接返回 + if (variableTableItemViewModel == null) + { + return; + } + + variableTableItemViewModel.DeviceId = device.Id; + var tableMenu = new MenuBeanDto() + { + Header = variableTableItemViewModel.Name, + Icon = SegoeFluentIcons.DataSense.Glyph, + TargetViewKey = "VariableTableView" + }; + int addVarTableId = await _wpfDataService.VariableTableDataService.AddVariableTable( + _mapper.Map(variableTableItemViewModel), + tableMenu, true); + + if (addVarTableId > 0) + { + variableTableItemViewModel.Id = addVarTableId; + if (_dataStorageService.Devices.TryGetValue(variableTableItemViewModel.DeviceId, out var deviceModel)) + { + variableTableItemViewModel.Device = deviceModel; + } + + _notificationService.ShowSuccess($"添加变量表成功:{variableTableItemViewModel.Name}"); + device.VariableTables.Add(variableTableItemViewModel); + } + else + { + _notificationService.ShowError($"添加变量表失败:{variableTableItemViewModel.Name}!!"); + } + } + catch (Exception ex) + { + _notificationService.ShowError($"添加变量表时发生错误: {ex.Message}", ex); + } + } + + [RelayCommand] + private async Task EditVariableTable(VariableTableItemViewModel variableTable) + { + if (variableTable == null) + { + _notificationService.ShowError("你没有选择任何变量表,请选择变量表后再点击编辑变量表"); + return; + } + + try + { + VariableTableDialogViewModel variableTableDialogViewModel + = new VariableTableDialogViewModel(variableTable) + { + PrimaryButText = "编辑变量表" + }; + // 显示变量表对话框 + VariableTableItemViewModel updatedVariableTable + = await _dialogService.ShowDialogAsync(variableTableDialogViewModel); + // 如果用户取消或对话框未返回变量表,则直接返回 + if (updatedVariableTable == null) + { + return; + } + + if (await _wpfDataService.VariableDataService.UpdateVariableTable(updatedVariableTable)) + { + _notificationService.ShowSuccess($"编辑变量表成功:{updatedVariableTable.Name}"); + + // Update the properties in the original variable table + variableTable.Name = updatedVariableTable.Name; + variableTable.Description = updatedVariableTable.Description; + variableTable.IsActive = updatedVariableTable.IsActive; + } + else + { + _notificationService.ShowError($"编辑变量表失败:{updatedVariableTable.Name}"); + } + } + catch (Exception e) + { + _notificationService.ShowError($"编辑变量表的过程中发生错误:{e.Message}", e); + } + } + + [RelayCommand] + private async Task DeleteVariableTable(VariableTableItemViewModel variableTable) + { + if (variableTable == null) + { + _notificationService.ShowError("你没有选择任何变量表,请选择变量表后再点击删除变量表"); + return; + } + + try + { + string message = $"确认要删除变量表名为:{variableTable.Name} \n\n此操作将同时删除该变量表下的所有变量数据,且无法恢复!"; + ConfirmDialogViewModel viewModel = new ConfirmDialogViewModel("删除变量表", message, "删除"); + if (await _dialogService.ShowDialogAsync(viewModel)) + { + var tableName = variableTable.Name; + if (await _wpfDataService.VariableDataService.DeleteVariableTable(variableTable, true)) + { + // Remove from parent device's collection + if (variableTable.Device != null && variableTable.Device.VariableTables.Contains(variableTable)) + { + variableTable.Device.VariableTables.Remove(variableTable); + } + _notificationService.ShowSuccess($"变量表:{tableName},删除成功。"); + } + else + { + _notificationService.ShowError($"变量表:{tableName},删除失败!!!"); + } + } + } + catch (Exception e) + { + _notificationService.ShowError($"删除变量表的过程中发生错误:{e.Message}", e); + } + } + private void OnDeviceIsActiveChanged(object? sender, bool isActive) { if (sender is DeviceItemViewModel deviceItemViewModel) diff --git a/DMS.WPF/Views/DevicesView.xaml b/DMS.WPF/Views/DevicesView.xaml index e6deabb..15d1c15 100644 --- a/DMS.WPF/Views/DevicesView.xaml +++ b/DMS.WPF/Views/DevicesView.xaml @@ -162,6 +162,8 @@ FontFamily="Segoe MDL2 Assets" FontSize="14" Style="{StaticResource DefaultButtonStyle}" + Command="{Binding DataContext.AddVariableTableCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" + CommandParameter="{Binding}" ToolTip="添加变量表" /> @@ -220,6 +222,8 @@ FontFamily="Segoe MDL2 Assets" FontSize="14" Style="{StaticResource DefaultButtonStyle}" + Command="{Binding DataContext.EditVariableTableCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" + CommandParameter="{Binding}" ToolTip="编辑变量表" />