参数配置界面弹窗添加
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using SettingModule.Views;
|
||||
using SettingModule.Views.Dialogs;
|
||||
using System.Reflection;
|
||||
|
||||
namespace SettingModule
|
||||
@@ -14,6 +15,10 @@ namespace SettingModule
|
||||
public void RegisterTypes(IContainerRegistry containerRegistry)
|
||||
{
|
||||
containerRegistry.RegisterForNavigation<SettingView>("SettingView");
|
||||
|
||||
// 设备连接配置弹窗
|
||||
containerRegistry.RegisterDialog<TCPConfigView>("TCPConfig");
|
||||
containerRegistry.RegisterDialog<SerialPortConfigView>("SerialPortConfig");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
164
SettingModule/ViewModels/Dialogs/SerialPortConfigViewModel.cs
Normal file
164
SettingModule/ViewModels/Dialogs/SerialPortConfigViewModel.cs
Normal file
@@ -0,0 +1,164 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO.Ports;
|
||||
using System.Linq;
|
||||
using System.Windows.Input;
|
||||
using UIShare.PubEvent;
|
||||
using UIShare.UIViewModel;
|
||||
using UIShare.ViewModelBase;
|
||||
|
||||
namespace SettingModule.ViewModels.Dialogs
|
||||
{
|
||||
/// <summary>
|
||||
/// 串口连接配置对话框 VM。
|
||||
/// 通过 DialogParameters 接收宿主 DeviceInfoModel;保存时把副本写回宿主。
|
||||
/// </summary>
|
||||
public class SerialPortConfigViewModel : DialogViewModelBase
|
||||
{
|
||||
#region 属性
|
||||
|
||||
private string _title = "串口连接配置";
|
||||
public string Title
|
||||
{
|
||||
get => _title;
|
||||
set => SetProperty(ref _title, value);
|
||||
}
|
||||
|
||||
private SerialPortConnectionConfig _config = new();
|
||||
public SerialPortConnectionConfig Config
|
||||
{
|
||||
get => _config;
|
||||
set => SetProperty(ref _config, value);
|
||||
}
|
||||
|
||||
/// <summary>当前可用串口列表(OnDialogOpened 刷新)。</summary>
|
||||
public ObservableCollection<string> AvailablePorts { get; } = new();
|
||||
|
||||
public ObservableCollection<int> CommonBaudRates { get; } = new()
|
||||
{
|
||||
1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200, 230400, 460800, 921600
|
||||
};
|
||||
|
||||
public ObservableCollection<int> DataBitsList { get; } = new() { 5, 6, 7, 8 };
|
||||
|
||||
public ObservableCollection<string> StopBitsList { get; } = new()
|
||||
{
|
||||
"One", "OnePointFive", "Two"
|
||||
};
|
||||
|
||||
public ObservableCollection<string> ParityList { get; } = new()
|
||||
{
|
||||
"None", "Odd", "Even", "Mark", "Space"
|
||||
};
|
||||
|
||||
public ObservableCollection<int> CommonTimeouts { get; } = new()
|
||||
{
|
||||
500, 1000, 2000, 3000, 5000, 10000
|
||||
};
|
||||
|
||||
private string _errorMessage = string.Empty;
|
||||
public string ErrorMessage
|
||||
{
|
||||
get => _errorMessage;
|
||||
set => SetProperty(ref _errorMessage, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 命令
|
||||
public ICommand SaveCommand { get; }
|
||||
public ICommand CancelCommand { get; }
|
||||
public ICommand RefreshPortsCommand { get; }
|
||||
#endregion
|
||||
|
||||
private DeviceInfoModel? _hostDevice;
|
||||
|
||||
public SerialPortConfigViewModel(IContainerProvider containerProvider) : base(containerProvider)
|
||||
{
|
||||
SaveCommand = new DelegateCommand(OnSave);
|
||||
CancelCommand = new DelegateCommand(OnCancel);
|
||||
RefreshPortsCommand = new DelegateCommand(RefreshPorts);
|
||||
}
|
||||
|
||||
private void RefreshPorts()
|
||||
{
|
||||
AvailablePorts.Clear();
|
||||
|
||||
|
||||
// 若当前选中的串口不在可用列表中,仍保留显示,便于离线编辑
|
||||
if (!string.IsNullOrEmpty(Config.PortName) && !AvailablePorts.Contains(Config.PortName))
|
||||
AvailablePorts.Insert(0, Config.PortName);
|
||||
}
|
||||
|
||||
private bool Validate(out string error)
|
||||
{
|
||||
error = string.Empty;
|
||||
if (string.IsNullOrWhiteSpace(Config.PortName))
|
||||
{
|
||||
error = "串口名称不能为空";
|
||||
return false;
|
||||
}
|
||||
if (Config.BaudRate <= 0)
|
||||
{
|
||||
error = "波特率必须大于 0";
|
||||
return false;
|
||||
}
|
||||
if (Config.DataBits < 5 || Config.DataBits > 8)
|
||||
{
|
||||
error = "数据位必须在 5 - 8 之间";
|
||||
return false;
|
||||
}
|
||||
if (Config.ReadTimeout < 0 || Config.WriteTimeout < 0)
|
||||
{
|
||||
error = "超时时间不能为负数";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void OnSave()
|
||||
{
|
||||
if (!Validate(out var error))
|
||||
{
|
||||
ErrorMessage = error;
|
||||
return;
|
||||
}
|
||||
ErrorMessage = string.Empty;
|
||||
|
||||
if (_hostDevice != null)
|
||||
{
|
||||
_hostDevice.SerialPortConfig ??= new SerialPortConnectionConfig();
|
||||
Config.CopyTo(_hostDevice.SerialPortConfig);
|
||||
_hostDevice.ConnectionType = "Serial";
|
||||
}
|
||||
|
||||
RequestClose.Invoke(ButtonResult.OK);
|
||||
}
|
||||
|
||||
private void OnCancel() => RequestClose.Invoke(ButtonResult.Cancel);
|
||||
|
||||
#region Prism Dialog 规范
|
||||
public override void OnDialogOpened(IDialogParameters parameters)
|
||||
{
|
||||
_eventAggregator.GetEvent<OverlayEvent>().Publish(true);
|
||||
|
||||
if (parameters.ContainsKey("Device"))
|
||||
{
|
||||
_hostDevice = parameters.GetValue<DeviceInfoModel>("Device");
|
||||
Title = $"串口连接配置 - {_hostDevice?.DeviceName}";
|
||||
Config = new SerialPortConnectionConfig(_hostDevice?.SerialPortConfig);
|
||||
}
|
||||
else if (parameters.ContainsKey("Config"))
|
||||
{
|
||||
Config = new SerialPortConnectionConfig(parameters.GetValue<SerialPortConnectionConfig>("Config"));
|
||||
}
|
||||
|
||||
RefreshPorts();
|
||||
}
|
||||
|
||||
public override void OnDialogClosed()
|
||||
{
|
||||
_eventAggregator.GetEvent<OverlayEvent>().Publish(false);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
139
SettingModule/ViewModels/Dialogs/TCPConfigViewModel.cs
Normal file
139
SettingModule/ViewModels/Dialogs/TCPConfigViewModel.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Windows.Input;
|
||||
using UIShare.PubEvent;
|
||||
using UIShare.UIViewModel;
|
||||
using UIShare.ViewModelBase;
|
||||
|
||||
|
||||
namespace SettingModule.ViewModels.Dialogs
|
||||
{
|
||||
/// <summary>
|
||||
/// TCP 连接配置对话框 VM。
|
||||
/// 通过 DialogParameters 接收宿主 DeviceInfoModel;保存时把副本写回宿主。
|
||||
/// </summary>
|
||||
public class TCPConfigViewModel : DialogViewModelBase
|
||||
{
|
||||
#region 属性
|
||||
|
||||
private string _title = "TCP 连接配置";
|
||||
public string Title
|
||||
{
|
||||
get => _title;
|
||||
set => SetProperty(ref _title, value);
|
||||
}
|
||||
|
||||
/// <summary>编辑用的副本,取消时不会污染宿主对象。</summary>
|
||||
private TcpConnectionConfig _config = new();
|
||||
public TcpConnectionConfig Config
|
||||
{
|
||||
get => _config;
|
||||
set => SetProperty(ref _config, value);
|
||||
}
|
||||
|
||||
/// <summary>常用 Modbus / 自定义协议端口建议。</summary>
|
||||
public ObservableCollection<int> CommonPorts { get; } = new()
|
||||
{
|
||||
502, 102, 80, 8080, 5020, 4840
|
||||
};
|
||||
|
||||
/// <summary>常用超时(毫秒)。</summary>
|
||||
public ObservableCollection<int> CommonTimeouts { get; } = new()
|
||||
{
|
||||
500, 1000, 2000, 3000, 5000, 10000
|
||||
};
|
||||
|
||||
private string _errorMessage = string.Empty;
|
||||
public string ErrorMessage
|
||||
{
|
||||
get => _errorMessage;
|
||||
set => SetProperty(ref _errorMessage, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 命令
|
||||
public ICommand SaveCommand { get; }
|
||||
public ICommand CancelCommand { get; }
|
||||
#endregion
|
||||
|
||||
// 用于保存时把副本回写到原对象
|
||||
private DeviceInfoModel? _hostDevice;
|
||||
|
||||
public TCPConfigViewModel(IContainerProvider containerProvider) : base(containerProvider)
|
||||
{
|
||||
SaveCommand = new DelegateCommand(OnSave);
|
||||
CancelCommand = new DelegateCommand(OnCancel);
|
||||
}
|
||||
|
||||
private bool Validate(out string error)
|
||||
{
|
||||
error = string.Empty;
|
||||
if (string.IsNullOrWhiteSpace(Config.IPAddress))
|
||||
{
|
||||
error = "IP 地址不能为空";
|
||||
return false;
|
||||
}
|
||||
if (!System.Net.IPAddress.TryParse(Config.IPAddress, out _))
|
||||
{
|
||||
error = "IP 地址格式不正确";
|
||||
return false;
|
||||
}
|
||||
if (Config.Port <= 0 || Config.Port > 65535)
|
||||
{
|
||||
error = "端口范围应在 1 - 65535";
|
||||
return false;
|
||||
}
|
||||
if (Config.SendTimeout < 0 || Config.ReceiveTimeout < 0)
|
||||
{
|
||||
error = "超时时间不能为负数";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void OnSave()
|
||||
{
|
||||
if (!Validate(out var error))
|
||||
{
|
||||
ErrorMessage = error;
|
||||
return;
|
||||
}
|
||||
ErrorMessage = string.Empty;
|
||||
|
||||
// 把副本写回宿主
|
||||
if (_hostDevice != null)
|
||||
{
|
||||
_hostDevice.TcpConfig ??= new TcpConnectionConfig();
|
||||
Config.CopyTo(_hostDevice.TcpConfig);
|
||||
_hostDevice.ConnectionType = "TCP";
|
||||
}
|
||||
|
||||
RequestClose.Invoke(ButtonResult.OK);
|
||||
}
|
||||
|
||||
private void OnCancel() => RequestClose.Invoke(ButtonResult.Cancel);
|
||||
|
||||
#region Prism Dialog 规范
|
||||
public override void OnDialogOpened(IDialogParameters parameters)
|
||||
{
|
||||
_eventAggregator.GetEvent<OverlayEvent>().Publish(true);
|
||||
|
||||
if (parameters.ContainsKey("Device"))
|
||||
{
|
||||
_hostDevice = parameters.GetValue<DeviceInfoModel>("Device");
|
||||
Title = $"TCP 连接配置 - {_hostDevice?.DeviceName}";
|
||||
Config = new TcpConnectionConfig(_hostDevice?.TcpConfig);
|
||||
}
|
||||
else if (parameters.ContainsKey("Config"))
|
||||
{
|
||||
Config = new TcpConnectionConfig(parameters.GetValue<TcpConnectionConfig>("Config"));
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnDialogClosed()
|
||||
{
|
||||
_eventAggregator.GetEvent<OverlayEvent>().Publish(false);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ using UIShare.GlobalVariable;
|
||||
using UIShare.PubEvent;
|
||||
using UIShare.UIViewModel;
|
||||
using UIShare.ViewModelBase;
|
||||
|
||||
namespace SettingModule.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
@@ -74,8 +73,16 @@ namespace SettingModule.ViewModels
|
||||
public ICommand RefreshCommand { get; }
|
||||
public ICommand SaveCommand { get; }
|
||||
public ICommand ResetCommand { get; }
|
||||
/// <summary>打开当前选中设备的连接配置对话框(按 ConnectionType 选择 TCP / 串口)。</summary>
|
||||
public ICommand OpenConnectionConfigCommand { get; }
|
||||
#endregion
|
||||
|
||||
/// <summary>支持的连接方式列表,供"连接方式"ComboBox 使用。</summary>
|
||||
public ObservableCollection<string> ConnectionTypes { get; } = new()
|
||||
{
|
||||
"None", "TCP", "Serial"
|
||||
};
|
||||
|
||||
public SettingViewModel(IContainerExtension container) : base(container)
|
||||
{
|
||||
_scope = container.CreateScope();
|
||||
@@ -89,6 +96,7 @@ namespace SettingModule.ViewModels
|
||||
RefreshCommand = new DelegateCommand(OnExpand);
|
||||
SaveCommand = new DelegateCommand(OnSave);
|
||||
ResetCommand = new DelegateCommand(OnReset);
|
||||
OpenConnectionConfigCommand = new DelegateCommand(OnOpenConnectionConfig);
|
||||
|
||||
// 进来默认选中第一个,让右侧不为空
|
||||
if (DeviceList.Count > 0)
|
||||
@@ -146,6 +154,40 @@ namespace SettingModule.ViewModels
|
||||
}
|
||||
StatusMessage = $"已重置设备 [{SelectedDevice.DeviceName}] 的配置";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打开"连接配置"对话框。按 SelectedDevice.ConnectionType 决定开 TCP 还是串口对话框。
|
||||
/// </summary>
|
||||
private void OnOpenConnectionConfig()
|
||||
{
|
||||
if (SelectedDevice == null)
|
||||
{
|
||||
StatusMessage = "请先在左侧选择设备";
|
||||
return;
|
||||
}
|
||||
|
||||
var dialogName = SelectedDevice.ConnectionType switch
|
||||
{
|
||||
"TCP" => "TCPConfig",
|
||||
"Serial" => "SerialPortConfig",
|
||||
_ => string.Empty
|
||||
};
|
||||
|
||||
if (string.IsNullOrEmpty(dialogName))
|
||||
{
|
||||
StatusMessage = "当前设备未配置连接方式(请先选择 TCP 或 Serial)";
|
||||
return;
|
||||
}
|
||||
|
||||
var p = new DialogParameters { { "Device", SelectedDevice } };
|
||||
_dialogService.ShowDialog(dialogName, p, r =>
|
||||
{
|
||||
if (r.Result == ButtonResult.OK)
|
||||
{
|
||||
StatusMessage = $"已更新 [{SelectedDevice.DeviceName}] 的连接参数({DateTime.Now:HH:mm:ss})";
|
||||
}
|
||||
});
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 导航
|
||||
|
||||
136
SettingModule/Views/Dialogs/SerialPortConfigView.xaml
Normal file
136
SettingModule/Views/Dialogs/SerialPortConfigView.xaml
Normal file
@@ -0,0 +1,136 @@
|
||||
<UserControl x:Class="SettingModule.Views.Dialogs.SerialPortConfigView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:SettingModule.Views.Dialogs"
|
||||
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
|
||||
xmlns:helpers="clr-namespace:UIShare.Helpers;assembly=UIShare"
|
||||
xmlns:converters="clr-namespace:UIShare.Converters;assembly=UIShare"
|
||||
mc:Ignorable="d"
|
||||
xmlns:prism="http://prismlibrary.com/"
|
||||
Background="White"
|
||||
prism:ViewModelLocator.AutoWireViewModel="True"
|
||||
Width="440"
|
||||
Height="430">
|
||||
<prism:Dialog.WindowStyle>
|
||||
<Style TargetType="Window">
|
||||
<Setter Property="ResizeMode" Value="NoResize"/>
|
||||
|
||||
<Setter Property="SizeToContent" Value="WidthAndHeight"/>
|
||||
</Style>
|
||||
</prism:Dialog.WindowStyle>
|
||||
|
||||
<UserControl.Resources>
|
||||
<converters:StringToVisibilityConverter x:Key="StringToVisibility"/>
|
||||
</UserControl.Resources>
|
||||
|
||||
<GroupBox Padding="12,8,12,8"
|
||||
Header="{Binding Title}"
|
||||
helpers:WindowDragHelper.EnableWindowDrag="True">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="*"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<!-- 表单 -->
|
||||
<Grid Grid.Row="0" Margin="0,4,0,0">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="100"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<TextBlock Grid.Row="0" Grid.Column="0"
|
||||
Text="串口名称:"
|
||||
VerticalAlignment="Center" Margin="0,6"/>
|
||||
<ComboBox Grid.Row="0" Grid.Column="1" Margin="0,6"
|
||||
IsEditable="True"
|
||||
ItemsSource="{Binding AvailablePorts}"
|
||||
Text="{Binding Config.PortName, UpdateSourceTrigger=PropertyChanged}"/>
|
||||
<Button Grid.Row="0" Grid.Column="2" Margin="6,6,0,6"
|
||||
Padding="8,0" Content="刷新"
|
||||
Command="{Binding RefreshPortsCommand}"/>
|
||||
|
||||
<TextBlock Grid.Row="1" Grid.Column="0"
|
||||
Text="波特率:"
|
||||
VerticalAlignment="Center" Margin="0,6"/>
|
||||
<ComboBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Margin="0,6"
|
||||
IsEditable="True"
|
||||
ItemsSource="{Binding CommonBaudRates}"
|
||||
Text="{Binding Config.BaudRate, UpdateSourceTrigger=PropertyChanged}"/>
|
||||
|
||||
<TextBlock Grid.Row="2" Grid.Column="0"
|
||||
Text="数据位:"
|
||||
VerticalAlignment="Center" Margin="0,6"/>
|
||||
<ComboBox Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" Margin="0,6"
|
||||
ItemsSource="{Binding DataBitsList}"
|
||||
SelectedItem="{Binding Config.DataBits}"/>
|
||||
|
||||
<TextBlock Grid.Row="3" Grid.Column="0"
|
||||
Text="停止位:"
|
||||
VerticalAlignment="Center" Margin="0,6"/>
|
||||
<ComboBox Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2" Margin="0,6"
|
||||
ItemsSource="{Binding StopBitsList}"
|
||||
SelectedItem="{Binding Config.StopBits}"/>
|
||||
|
||||
<TextBlock Grid.Row="4" Grid.Column="0"
|
||||
Text="校验位:"
|
||||
VerticalAlignment="Center" Margin="0,6"/>
|
||||
<ComboBox Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" Margin="0,6"
|
||||
ItemsSource="{Binding ParityList}"
|
||||
SelectedItem="{Binding Config.Parity}"/>
|
||||
|
||||
<TextBlock Grid.Row="5" Grid.Column="0"
|
||||
Text="读取超时(ms):"
|
||||
VerticalAlignment="Center" Margin="0,6"/>
|
||||
<ComboBox Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="2" Margin="0,6"
|
||||
IsEditable="True"
|
||||
ItemsSource="{Binding CommonTimeouts}"
|
||||
Text="{Binding Config.ReadTimeout, UpdateSourceTrigger=PropertyChanged}"/>
|
||||
|
||||
<TextBlock Grid.Row="6" Grid.Column="0"
|
||||
Text="写入超时(ms):"
|
||||
VerticalAlignment="Center" Margin="0,6"/>
|
||||
<ComboBox Grid.Row="6" Grid.Column="1" Grid.ColumnSpan="2" Margin="0,6"
|
||||
IsEditable="True"
|
||||
ItemsSource="{Binding CommonTimeouts}"
|
||||
Text="{Binding Config.WriteTimeout, UpdateSourceTrigger=PropertyChanged}"/>
|
||||
</Grid>
|
||||
|
||||
<!-- 错误提示 -->
|
||||
<TextBlock Grid.Row="1"
|
||||
Margin="0,8,0,0"
|
||||
Foreground="#D32F2F"
|
||||
TextWrapping="Wrap"
|
||||
Text="{Binding ErrorMessage}"
|
||||
Visibility="{Binding ErrorMessage, Converter={StaticResource StringToVisibility}}"/>
|
||||
|
||||
<!-- 按钮 -->
|
||||
<StackPanel Grid.Row="2"
|
||||
Orientation="Horizontal"
|
||||
HorizontalAlignment="Right"
|
||||
Margin="0,12,0,0">
|
||||
<Button Content="取消"
|
||||
Width="80" Padding="0,4"
|
||||
Command="{Binding CancelCommand}"/>
|
||||
<Button Content="保存"
|
||||
Width="80" Padding="0,4"
|
||||
Margin="10,0,0,0"
|
||||
IsDefault="True"
|
||||
Command="{Binding SaveCommand}"/>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</GroupBox>
|
||||
</UserControl>
|
||||
28
SettingModule/Views/Dialogs/SerialPortConfigView.xaml.cs
Normal file
28
SettingModule/Views/Dialogs/SerialPortConfigView.xaml.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace SettingModule.Views.Dialogs
|
||||
{
|
||||
/// <summary>
|
||||
/// SerialPortConfigView.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class SerialPortConfigView : UserControl
|
||||
{
|
||||
public SerialPortConfigView()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
105
SettingModule/Views/Dialogs/TCPConfigView.xaml
Normal file
105
SettingModule/Views/Dialogs/TCPConfigView.xaml
Normal file
@@ -0,0 +1,105 @@
|
||||
<UserControl x:Class="SettingModule.Views.Dialogs.TCPConfigView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:SettingModule.Views.Dialogs"
|
||||
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
|
||||
xmlns:helpers="clr-namespace:UIShare.Helpers;assembly=UIShare"
|
||||
xmlns:converters="clr-namespace:UIShare.Converters;assembly=UIShare"
|
||||
mc:Ignorable="d"
|
||||
xmlns:prism="http://prismlibrary.com/"
|
||||
Background="White"
|
||||
prism:ViewModelLocator.AutoWireViewModel="True"
|
||||
Width="420"
|
||||
Height="320">
|
||||
<prism:Dialog.WindowStyle>
|
||||
<Style TargetType="Window">
|
||||
<Setter Property="ResizeMode" Value="NoResize"/>
|
||||
<Setter Property="SizeToContent" Value="WidthAndHeight"/>
|
||||
</Style>
|
||||
</prism:Dialog.WindowStyle>
|
||||
|
||||
<UserControl.Resources>
|
||||
<converters:StringToVisibilityConverter x:Key="StringToVisibility"/>
|
||||
</UserControl.Resources>
|
||||
|
||||
<GroupBox Padding="12,8,12,8"
|
||||
Header="{Binding Title}"
|
||||
helpers:WindowDragHelper.EnableWindowDrag="True">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="*"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<!-- 表单 -->
|
||||
<Grid Grid.Row="0" Margin="0,4,0,0">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="100"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<TextBlock Grid.Row="0" Grid.Column="0"
|
||||
Text="IP 地址:"
|
||||
VerticalAlignment="Center" Margin="0,6"/>
|
||||
<TextBox Grid.Row="0" Grid.Column="1" Margin="0,6"
|
||||
Text="{Binding Config.IPAddress, UpdateSourceTrigger=PropertyChanged}"/>
|
||||
|
||||
<TextBlock Grid.Row="1" Grid.Column="0"
|
||||
Text="端口:"
|
||||
VerticalAlignment="Center" Margin="0,6"/>
|
||||
<ComboBox Grid.Row="1" Grid.Column="1" Margin="0,6"
|
||||
IsEditable="True"
|
||||
ItemsSource="{Binding CommonPorts}"
|
||||
Text="{Binding Config.Port, UpdateSourceTrigger=PropertyChanged}"/>
|
||||
|
||||
<TextBlock Grid.Row="2" Grid.Column="0"
|
||||
Text="发送超时(ms):"
|
||||
VerticalAlignment="Center" Margin="0,6"/>
|
||||
<ComboBox Grid.Row="2" Grid.Column="1" Margin="0,6"
|
||||
IsEditable="True"
|
||||
ItemsSource="{Binding CommonTimeouts}"
|
||||
Text="{Binding Config.SendTimeout, UpdateSourceTrigger=PropertyChanged}"/>
|
||||
|
||||
<TextBlock Grid.Row="3" Grid.Column="0"
|
||||
Text="接收超时(ms):"
|
||||
VerticalAlignment="Center" Margin="0,6"/>
|
||||
<ComboBox Grid.Row="3" Grid.Column="1" Margin="0,6"
|
||||
IsEditable="True"
|
||||
ItemsSource="{Binding CommonTimeouts}"
|
||||
Text="{Binding Config.ReceiveTimeout, UpdateSourceTrigger=PropertyChanged}"/>
|
||||
</Grid>
|
||||
|
||||
<!-- 错误提示 -->
|
||||
<TextBlock Grid.Row="1"
|
||||
Margin="0,8,0,0"
|
||||
Foreground="#D32F2F"
|
||||
TextWrapping="Wrap"
|
||||
Text="{Binding ErrorMessage}"
|
||||
Visibility="{Binding ErrorMessage, Converter={StaticResource StringToVisibility}}"/>
|
||||
|
||||
<!-- 按钮 -->
|
||||
<StackPanel Grid.Row="2"
|
||||
Orientation="Horizontal"
|
||||
HorizontalAlignment="Right"
|
||||
Margin="0,12,0,0">
|
||||
<Button Content="取消"
|
||||
Width="80" Padding="0,4"
|
||||
Command="{Binding CancelCommand}"/>
|
||||
<Button Content="保存"
|
||||
Width="80" Padding="0,4"
|
||||
Margin="10,0,0,0"
|
||||
IsDefault="True"
|
||||
Command="{Binding SaveCommand}"/>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</GroupBox>
|
||||
</UserControl>
|
||||
28
SettingModule/Views/Dialogs/TCPConfigView.xaml.cs
Normal file
28
SettingModule/Views/Dialogs/TCPConfigView.xaml.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace SettingModule.Views.Dialogs
|
||||
{
|
||||
/// <summary>
|
||||
/// TCPConfigView.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class TCPConfigView : UserControl
|
||||
{
|
||||
public TCPConfigView()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -290,8 +290,84 @@
|
||||
Margin="0,0,0,12">
|
||||
<StackPanel>
|
||||
<TextBlock Text="连接参数" FontWeight="Bold" FontSize="14" Margin="0,0,0,10"/>
|
||||
<TextBlock Foreground="#888" FontSize="12" TextWrapping="Wrap"
|
||||
Text="此处将根据设备类型显示对应的连接参数(如串口波特率、CAN 比特率、IP 端口等),当前为占位区域。"/>
|
||||
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="100"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<TextBlock Grid.Row="0" Grid.Column="0"
|
||||
Text="连接方式:"
|
||||
VerticalAlignment="Center"
|
||||
Margin="0,4"/>
|
||||
<ComboBox Grid.Row="0" Grid.Column="1"
|
||||
Margin="0,4"
|
||||
ItemsSource="{Binding ConnectionTypes}"
|
||||
SelectedItem="{Binding SelectedDevice.ConnectionType}"/>
|
||||
<Button Grid.Row="0" Grid.Column="2"
|
||||
Content="配置..."
|
||||
Margin="8,4,0,4" Padding="14,2"
|
||||
Command="{Binding OpenConnectionConfigCommand}"/>
|
||||
|
||||
<!-- TCP 参数预览 -->
|
||||
<StackPanel Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"
|
||||
Margin="0,4,0,0" Orientation="Horizontal">
|
||||
<StackPanel.Style>
|
||||
<Style TargetType="StackPanel">
|
||||
<Setter Property="Visibility" Value="Collapsed"/>
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding SelectedDevice.ConnectionType}" Value="TCP">
|
||||
<Setter Property="Visibility" Value="Visible"/>
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</StackPanel.Style>
|
||||
<TextBlock Foreground="#666" FontSize="12">
|
||||
<Run Text="IP:"/>
|
||||
<Run Text="{Binding SelectedDevice.TcpConfig.IPAddress}"/>
|
||||
<Run Text=" 端口:"/>
|
||||
<Run Text="{Binding SelectedDevice.TcpConfig.Port}"/>
|
||||
<Run Text=" 发送/接收超时:"/>
|
||||
<Run Text="{Binding SelectedDevice.TcpConfig.SendTimeout}"/>
|
||||
<Run Text="/"/>
|
||||
<Run Text="{Binding SelectedDevice.TcpConfig.ReceiveTimeout}"/>
|
||||
<Run Text=" ms"/>
|
||||
</TextBlock>
|
||||
</StackPanel>
|
||||
|
||||
<!-- 串口参数预览 -->
|
||||
<StackPanel Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"
|
||||
Margin="0,4,0,0" Orientation="Horizontal">
|
||||
<StackPanel.Style>
|
||||
<Style TargetType="StackPanel">
|
||||
<Setter Property="Visibility" Value="Collapsed"/>
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding SelectedDevice.ConnectionType}" Value="Serial">
|
||||
<Setter Property="Visibility" Value="Visible"/>
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</StackPanel.Style>
|
||||
<TextBlock Foreground="#666" FontSize="12">
|
||||
<Run Text="串口:"/>
|
||||
<Run Text="{Binding SelectedDevice.SerialPortConfig.PortName}"/>
|
||||
<Run Text=" 波特率:"/>
|
||||
<Run Text="{Binding SelectedDevice.SerialPortConfig.BaudRate}"/>
|
||||
<Run Text=" 数据/停止/校验:"/>
|
||||
<Run Text="{Binding SelectedDevice.SerialPortConfig.DataBits}"/>
|
||||
<Run Text="/"/>
|
||||
<Run Text="{Binding SelectedDevice.SerialPortConfig.StopBits}"/>
|
||||
<Run Text="/"/>
|
||||
<Run Text="{Binding SelectedDevice.SerialPortConfig.Parity}"/>
|
||||
</TextBlock>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</StackPanel>
|
||||
</Border>
|
||||
|
||||
|
||||
144
UIShare/UIViewModel/ConnectionConfig.cs
Normal file
144
UIShare/UIViewModel/ConnectionConfig.cs
Normal file
@@ -0,0 +1,144 @@
|
||||
using Prism.Mvvm;
|
||||
|
||||
namespace UIShare.UIViewModel
|
||||
{
|
||||
/// <summary>
|
||||
/// TCP 连接配置(与 DeviceCommand.Base.Tcp 保持字段一致)。
|
||||
/// </summary>
|
||||
public class TcpConnectionConfig : BindableBase
|
||||
{
|
||||
private string _ipAddress = "127.0.0.1";
|
||||
public string IPAddress
|
||||
{
|
||||
get => _ipAddress;
|
||||
set => SetProperty(ref _ipAddress, value);
|
||||
}
|
||||
|
||||
private int _port = 502;
|
||||
public int Port
|
||||
{
|
||||
get => _port;
|
||||
set => SetProperty(ref _port, value);
|
||||
}
|
||||
|
||||
private int _sendTimeout = 3000;
|
||||
public int SendTimeout
|
||||
{
|
||||
get => _sendTimeout;
|
||||
set => SetProperty(ref _sendTimeout, value);
|
||||
}
|
||||
|
||||
private int _receiveTimeout = 3000;
|
||||
public int ReceiveTimeout
|
||||
{
|
||||
get => _receiveTimeout;
|
||||
set => SetProperty(ref _receiveTimeout, value);
|
||||
}
|
||||
|
||||
public TcpConnectionConfig() { }
|
||||
|
||||
/// <summary>拷贝构造,用于对话框编辑副本。</summary>
|
||||
public TcpConnectionConfig(TcpConnectionConfig? src)
|
||||
{
|
||||
if (src == null) return;
|
||||
IPAddress = src.IPAddress;
|
||||
Port = src.Port;
|
||||
SendTimeout = src.SendTimeout;
|
||||
ReceiveTimeout = src.ReceiveTimeout;
|
||||
}
|
||||
|
||||
/// <summary>把字段拷回目标对象(保存时用)。</summary>
|
||||
public void CopyTo(TcpConnectionConfig? dst)
|
||||
{
|
||||
if (dst == null) return;
|
||||
dst.IPAddress = IPAddress;
|
||||
dst.Port = Port;
|
||||
dst.SendTimeout = SendTimeout;
|
||||
dst.ReceiveTimeout = ReceiveTimeout;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 串口连接配置(与 DeviceCommand.Base.Serial_Port 保持字段一致)。
|
||||
/// StopBits / Parity 用字符串保存,避免 UIShare 引入 System.IO.Ports 依赖。
|
||||
/// </summary>
|
||||
public class SerialPortConnectionConfig : BindableBase
|
||||
{
|
||||
private string _portName = "COM1";
|
||||
public string PortName
|
||||
{
|
||||
get => _portName;
|
||||
set => SetProperty(ref _portName, value);
|
||||
}
|
||||
|
||||
private int _baudRate = 9600;
|
||||
public int BaudRate
|
||||
{
|
||||
get => _baudRate;
|
||||
set => SetProperty(ref _baudRate, value);
|
||||
}
|
||||
|
||||
private int _dataBits = 8;
|
||||
public int DataBits
|
||||
{
|
||||
get => _dataBits;
|
||||
set => SetProperty(ref _dataBits, value);
|
||||
}
|
||||
|
||||
// 取值:"One" / "OnePointFive" / "Two" / "None"
|
||||
private string _stopBits = "One";
|
||||
public string StopBits
|
||||
{
|
||||
get => _stopBits;
|
||||
set => SetProperty(ref _stopBits, value);
|
||||
}
|
||||
|
||||
// 取值:"None" / "Odd" / "Even" / "Mark" / "Space"
|
||||
private string _parity = "None";
|
||||
public string Parity
|
||||
{
|
||||
get => _parity;
|
||||
set => SetProperty(ref _parity, value);
|
||||
}
|
||||
|
||||
private int _readTimeout = 3000;
|
||||
public int ReadTimeout
|
||||
{
|
||||
get => _readTimeout;
|
||||
set => SetProperty(ref _readTimeout, value);
|
||||
}
|
||||
|
||||
private int _writeTimeout = 3000;
|
||||
public int WriteTimeout
|
||||
{
|
||||
get => _writeTimeout;
|
||||
set => SetProperty(ref _writeTimeout, value);
|
||||
}
|
||||
|
||||
public SerialPortConnectionConfig() { }
|
||||
|
||||
public SerialPortConnectionConfig(SerialPortConnectionConfig? src)
|
||||
{
|
||||
if (src == null) return;
|
||||
PortName = src.PortName;
|
||||
BaudRate = src.BaudRate;
|
||||
DataBits = src.DataBits;
|
||||
StopBits = src.StopBits;
|
||||
Parity = src.Parity;
|
||||
ReadTimeout = src.ReadTimeout;
|
||||
WriteTimeout = src.WriteTimeout;
|
||||
}
|
||||
|
||||
public void CopyTo(SerialPortConnectionConfig? dst)
|
||||
{
|
||||
if (dst == null) return;
|
||||
dst.PortName = PortName;
|
||||
dst.BaudRate = BaudRate;
|
||||
dst.DataBits = DataBits;
|
||||
dst.StopBits = StopBits;
|
||||
dst.Parity = Parity;
|
||||
dst.ReadTimeout = ReadTimeout;
|
||||
dst.WriteTimeout = WriteTimeout;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,31 @@ namespace UIShare.UIViewModel
|
||||
set => SetProperty(ref _isConnected, value);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 连接方式:"None" / "TCP" / "Serial"。
|
||||
/// 用于决定 SettingView 上"配置..."按钮打开哪一个对话框。
|
||||
/// </summary>
|
||||
private string _connectionType = "None";
|
||||
public string ConnectionType
|
||||
{
|
||||
get => _connectionType;
|
||||
set => SetProperty(ref _connectionType, value);
|
||||
}
|
||||
|
||||
/// <summary>TCP 连接参数(首次访问时自动初始化,便于 XAML 直接绑定)。</summary>
|
||||
private TcpConnectionConfig _tcpConfig = new();
|
||||
public TcpConnectionConfig TcpConfig
|
||||
{
|
||||
get => _tcpConfig;
|
||||
set => SetProperty(ref _tcpConfig, value);
|
||||
}
|
||||
|
||||
/// <summary>串口连接参数(首次访问时自动初始化,便于 XAML 直接绑定)。</summary>
|
||||
private SerialPortConnectionConfig _serialPortConfig = new();
|
||||
public SerialPortConnectionConfig SerialPortConfig
|
||||
{
|
||||
get => _serialPortConfig;
|
||||
set => SetProperty(ref _serialPortConfig, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user