From d923b6a1169afbc05ee6d7feded03a58682ffbd0 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Sun, 14 Sep 2025 20:46:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E7=9A=84=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DMS.WPF/App.xaml | 7 + DMS.WPF/App.xaml.cs | 4 + DMS.WPF/Converters/BoolToColorConverter.cs | 51 +++ DMS.WPF/Converters/BoolToStringConverter.cs | 39 ++ .../Converters/BoolToVisibilityConverter.cs | 33 ++ .../Converters/CountToVisibilityConverter.cs | 29 ++ .../ViewModels/Items/DeviceItemViewModel.cs | 6 + DMS.WPF/ViewModels/VariableTableViewModel.cs | 36 +- DMS.WPF/Views/DevicesView.xaml | 375 +++++++++++++----- DMS.WPF/Views/Dialogs/DeviceDialog.xaml | 38 +- 10 files changed, 469 insertions(+), 149 deletions(-) create mode 100644 DMS.WPF/Converters/BoolToColorConverter.cs create mode 100644 DMS.WPF/Converters/BoolToStringConverter.cs create mode 100644 DMS.WPF/Converters/BoolToVisibilityConverter.cs create mode 100644 DMS.WPF/Converters/CountToVisibilityConverter.cs diff --git a/DMS.WPF/App.xaml b/DMS.WPF/App.xaml index da96fa7..b87ada4 100644 --- a/DMS.WPF/App.xaml +++ b/DMS.WPF/App.xaml @@ -26,6 +26,13 @@ + + + + + + + diff --git a/DMS.WPF/App.xaml.cs b/DMS.WPF/App.xaml.cs index 0dbae03..1a66eb0 100644 --- a/DMS.WPF/App.xaml.cs +++ b/DMS.WPF/App.xaml.cs @@ -275,6 +275,10 @@ public partial class App : System.Windows.Application services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); // 注册事件服务 services.AddSingleton(); diff --git a/DMS.WPF/Converters/BoolToColorConverter.cs b/DMS.WPF/Converters/BoolToColorConverter.cs new file mode 100644 index 0000000..841304e --- /dev/null +++ b/DMS.WPF/Converters/BoolToColorConverter.cs @@ -0,0 +1,51 @@ +using System; +using System.Globalization; +using System.Windows.Data; +using System.Windows.Media; + +namespace DMS.WPF.Converters +{ + /// + /// 布尔值到颜色转换器。根据布尔值返回不同的颜色。 + /// 参数格式: "TrueColor;FalseColor" + /// + public class BoolToColorConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is bool boolValue) + { + string param = parameter as string; + if (!string.IsNullOrEmpty(param)) + { + string[] colors = param.Split(';'); + if (colors.Length == 2) + { + try + { + string colorString = boolValue ? colors[0] : colors[1]; + Color color = (Color)ColorConverter.ConvertFromString(colorString); + return new SolidColorBrush(color); + } + catch (FormatException) + { + // 如果颜色格式无效,返回默认颜色 + } + } + } + + // 默认颜色 + Color defaultColor = boolValue ? Colors.Green : Colors.Red; + return new SolidColorBrush(defaultColor); + } + + // 如果值不是布尔值,返回红色 + return new SolidColorBrush(Colors.Red); + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/DMS.WPF/Converters/BoolToStringConverter.cs b/DMS.WPF/Converters/BoolToStringConverter.cs new file mode 100644 index 0000000..d729617 --- /dev/null +++ b/DMS.WPF/Converters/BoolToStringConverter.cs @@ -0,0 +1,39 @@ +using System; +using System.Globalization; +using System.Windows.Data; + +namespace DMS.WPF.Converters +{ + /// + /// 布尔值到字符串转换器。根据布尔值返回不同的字符串。 + /// 参数格式: "TrueString;FalseString" + /// + public class BoolToStringConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is bool boolValue) + { + string param = parameter as string; + if (!string.IsNullOrEmpty(param)) + { + string[] strings = param.Split(';'); + if (strings.Length == 2) + { + return boolValue ? strings[0] : strings[1]; + } + } + + // 默认返回 + return boolValue ? "是" : "否"; + } + + return "未知"; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/DMS.WPF/Converters/BoolToVisibilityConverter.cs b/DMS.WPF/Converters/BoolToVisibilityConverter.cs new file mode 100644 index 0000000..95e2cb2 --- /dev/null +++ b/DMS.WPF/Converters/BoolToVisibilityConverter.cs @@ -0,0 +1,33 @@ +using System; +using System.Globalization; +using System.Windows; +using System.Windows.Data; + +namespace DMS.WPF.Converters +{ + /// + /// 布尔值到可见性转换器。当绑定的布尔值为true时,返回Visible,否则返回Collapsed。 + /// + public class BoolToVisibilityConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is bool boolValue) + { + return boolValue ? Visibility.Visible : Visibility.Collapsed; + } + + return Visibility.Collapsed; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is Visibility visibility) + { + return visibility == Visibility.Visible; + } + + return false; + } + } +} \ No newline at end of file diff --git a/DMS.WPF/Converters/CountToVisibilityConverter.cs b/DMS.WPF/Converters/CountToVisibilityConverter.cs new file mode 100644 index 0000000..3383a12 --- /dev/null +++ b/DMS.WPF/Converters/CountToVisibilityConverter.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections; +using System.Globalization; +using System.Windows; +using System.Windows.Data; + +namespace DMS.WPF.Converters +{ + /// + /// 计数到可见性转换器。当集合为空时,返回Visible,否则返回Collapsed。 + /// + public class CountToVisibilityConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is ICollection collection) + { + return collection.Count == 0 ? Visibility.Visible : Visibility.Collapsed; + } + + return Visibility.Visible; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs b/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs index c8557f2..9651ff9 100644 --- a/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs +++ b/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs @@ -97,5 +97,11 @@ public partial class DeviceItemViewModel : ObservableObject } public ObservableCollection VariableTables { get; set; } = new(); + + [ObservableProperty] + private bool _isConnected; + + [ObservableProperty] + private string _connectionStatus = "未连接"; } diff --git a/DMS.WPF/ViewModels/VariableTableViewModel.cs b/DMS.WPF/ViewModels/VariableTableViewModel.cs index 234ddb4..02d25c1 100644 --- a/DMS.WPF/ViewModels/VariableTableViewModel.cs +++ b/DMS.WPF/ViewModels/VariableTableViewModel.cs @@ -519,40 +519,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable } } - /// - /// 修改选定变量的OPC UA更新方式(轮询或订阅)。 - /// - /// 要修改更新方式的变量数据列表。 - [RelayCommand] - public async Task ModifyOpcUaUpdateType(IList variablesToChange) - { - // // 过滤出有效的VariableData对象 - // var validVariables = variablesToChange?.OfType() - // .ToList(); - // - // if (validVariables == null || !validVariables.Any()) - // { - // NotificationHelper.ShowInfo("请选择要修改更新方式的OPC UA变量"); - // return; - // } - // - // - // // 显示更新方式选择对话框 - // var newUpdateType = await _dialogService.ShowOpcUaUpdateTypeDialog(); - // if (newUpdateType.HasValue) - // { - // // 更新所有选定变量的更新方式 - // foreach (var variable in validVariables) - // { - // variable.OpcUaUpdateType = newUpdateType.Value; - // } - // - // // 批量更新数据库 - // await _varDataRepository.UpdateAsync(validVariables); - // NotificationHelper.ShowSuccess($"已成功为 {validVariables.Count} 个变量更新OPC UA更新方式。"); - // } - } - + /// /// 为选定的变量添加MQTT服务器。 /// 此命令通常绑定到UI中的“添加MQTT服务器”按钮。 @@ -751,6 +718,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable // 批量更新数据库中的变量数据 var variableDtos = _mapper.Map>(validVariables); var updateResult = await _variableAppService.UpdateVariablesAsync(variableDtos); + if (updateResult > 0) { diff --git a/DMS.WPF/Views/DevicesView.xaml b/DMS.WPF/Views/DevicesView.xaml index 6a42db2..7842814 100644 --- a/DMS.WPF/Views/DevicesView.xaml +++ b/DMS.WPF/Views/DevicesView.xaml @@ -14,110 +14,291 @@ mc:Ignorable="d"> + + + + + + + + - + - + - - - - + - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - diff --git a/DMS.WPF/Views/Dialogs/DeviceDialog.xaml b/DMS.WPF/Views/Dialogs/DeviceDialog.xaml index d68dc08..e116d35 100644 --- a/DMS.WPF/Views/Dialogs/DeviceDialog.xaml +++ b/DMS.WPF/Views/Dialogs/DeviceDialog.xaml @@ -52,14 +52,25 @@ - - + Margin="0,15,0,0" + hc:InfoElement.Title="通讯协议:" + IsEnabled="{Binding IsAddMode}" + ItemsSource="{Binding Source={StaticResource ProtocolType}}" + SelectedItem="{Binding Device.Protocol}"> + + + + + + + - - - - - - - + hc:InfoElement.Title="设备描述:" + Text="{Binding Device.Description, UpdateSourceTrigger=PropertyChanged}" />