示波器修改:visa连接方式+截图保存
This commit is contained in:
@@ -19,6 +19,7 @@ namespace SettingModule
|
||||
|
||||
// 设备连接配置弹窗
|
||||
containerRegistry.RegisterDialog<TCPConfigView>("TCPConfig");
|
||||
containerRegistry.RegisterDialog<VisaConfigView>("VisaConfig");
|
||||
containerRegistry.RegisterDialog<SerialPortConfigView>("SerialPortConfig");
|
||||
containerRegistry.RegisterDialog<CANConfigView>("CANConfig");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Windows.Input;
|
||||
using UIShare.PubEvent;
|
||||
using UIShare.UIViewModel;
|
||||
using UIShare.ViewModelBase;
|
||||
|
||||
|
||||
namespace SettingModule.ViewModels.Dialogs
|
||||
{
|
||||
/// <summary>
|
||||
/// VISA 连接配置对话框 VM。
|
||||
/// 通过 DialogParameters 接收宿主 DeviceInfoVM;保存时把副本写回宿主。
|
||||
/// </summary>
|
||||
public class VisaConfigViewModel : DialogViewModelBase
|
||||
{
|
||||
#region 属性
|
||||
|
||||
private string _title = "VISA 连接配置";
|
||||
public string Title
|
||||
{
|
||||
get => _title;
|
||||
set => SetProperty(ref _title, value);
|
||||
}
|
||||
|
||||
/// <summary>编辑用的副本,取消时不会污染宿主对象。</summary>
|
||||
private VisaConfigVM _config = new();
|
||||
public VisaConfigVM Config
|
||||
{
|
||||
get => _config;
|
||||
set => SetProperty(ref _config, value);
|
||||
}
|
||||
|
||||
/// <summary>常用超时(毫秒)。</summary>
|
||||
public ObservableCollection<int> CommonTimeouts { get; } = new()
|
||||
{
|
||||
500, 1000, 2000, 3000, 5000, 10000
|
||||
};
|
||||
|
||||
/// <summary>常见 VISA 资源字符串模板,便于快速填写(可直接编辑)。</summary>
|
||||
public ObservableCollection<string> ResourceTemplates { get; } = new()
|
||||
{
|
||||
"TCPIP0::192.168.20.125::inst0::INSTR",
|
||||
"TCPIP0::192.168.20.125::5025::SOCKET",
|
||||
"USB0::0x0699::0x0408::C012345::INSTR",
|
||||
"GPIB0::7::INSTR",
|
||||
"ASRL1::INSTR"
|
||||
};
|
||||
|
||||
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 DeviceInfoVM? _hostDevice;
|
||||
|
||||
public VisaConfigViewModel(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.ResourceName))
|
||||
{
|
||||
error = "VISA 资源字符串不能为空";
|
||||
return false;
|
||||
}
|
||||
if (!Config.ResourceName.Contains("::"))
|
||||
{
|
||||
error = "资源字符串格式不正确,应形如 TCPIP0::192.168.0.10::inst0::INSTR";
|
||||
return false;
|
||||
}
|
||||
if (Config.OpenTimeout < 0 || Config.IOTimeout < 0)
|
||||
{
|
||||
error = "超时时间不能为负数";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void OnSave()
|
||||
{
|
||||
if (!Validate(out var error))
|
||||
{
|
||||
ErrorMessage = error;
|
||||
return;
|
||||
}
|
||||
ErrorMessage = string.Empty;
|
||||
|
||||
// 把副本写回宿主
|
||||
if (_hostDevice != null)
|
||||
{
|
||||
_hostDevice.VisaConfig ??= new VisaConfigVM();
|
||||
Config.CopyTo(_hostDevice.VisaConfig);
|
||||
_hostDevice.ConnectionType = "Visa";
|
||||
}
|
||||
|
||||
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<DeviceInfoVM>("Device");
|
||||
Title = $"VISA 连接配置 - {_hostDevice?.DeviceName}";
|
||||
Config = new VisaConfigVM(_hostDevice?.VisaConfig);
|
||||
}
|
||||
else if (parameters.ContainsKey("Config"))
|
||||
{
|
||||
Config = new VisaConfigVM(parameters.GetValue<VisaConfigVM>("Config"));
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnDialogClosed()
|
||||
{
|
||||
_eventAggregator.GetEvent<OverlayEvent>().Publish(false);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -59,7 +59,7 @@ namespace SettingModule.ViewModels
|
||||
|
||||
public ObservableCollection<string> ConnectionTypes { get; } = new()
|
||||
{
|
||||
"None", "Tcp", "Serial", "CAN"
|
||||
"None", "Tcp", "Visa", "Serial", "CAN"
|
||||
};
|
||||
#endregion
|
||||
#region 命令
|
||||
@@ -163,6 +163,7 @@ namespace SettingModule.ViewModels
|
||||
var dialogName = SelectedDevice.ConnectionType switch
|
||||
{
|
||||
"Tcp" => "TCPConfig",
|
||||
"Visa" => "VisaConfig",
|
||||
"Serial" => "SerialPortConfig",
|
||||
"CAN" => "CANConfig",
|
||||
_ => string.Empty
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
<UserControl x:Class="SettingModule.Views.Dialogs.VisaConfigView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
|
||||
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="500"
|
||||
Height="300">
|
||||
<prism:Dialog.WindowStyle>
|
||||
<Style BasedOn="{StaticResource DialogUserManageStyle}"
|
||||
TargetType="Window" />
|
||||
</prism:Dialog.WindowStyle>
|
||||
|
||||
<UserControl.Resources>
|
||||
<converters:StringToVisibilityConverter x:Key="StringToVisibility"/>
|
||||
</UserControl.Resources>
|
||||
|
||||
<GroupBox Padding="12,8,12,8"
|
||||
helpers:WindowDragHelper.EnableWindowDrag="True">
|
||||
<GroupBox.Header>
|
||||
<Grid Margin="0">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition />
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition />
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Text="{Binding Title}"
|
||||
Foreground="White"
|
||||
VerticalAlignment="Center"
|
||||
Margin="5,0,10,0" />
|
||||
</Grid>
|
||||
</GroupBox.Header>
|
||||
<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="110"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid.RowDefinitions>
|
||||
<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"
|
||||
materialDesign:HintAssist.Hint="TCPIP0::IP::inst0::INSTR"
|
||||
ItemsSource="{Binding ResourceTemplates}"
|
||||
Text="{Binding Config.ResourceName, UpdateSourceTrigger=PropertyChanged}"/>
|
||||
|
||||
<TextBlock Grid.Row="1" Grid.Column="0"
|
||||
Text="打开超时(ms):"
|
||||
VerticalAlignment="Center" Margin="0,6"/>
|
||||
<ComboBox Grid.Row="1" Grid.Column="1" Margin="0,6"
|
||||
IsEditable="True"
|
||||
materialDesign:HintAssist.Hint=""
|
||||
ItemsSource="{Binding CommonTimeouts}"
|
||||
Text="{Binding Config.OpenTimeout, 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"
|
||||
materialDesign:HintAssist.Hint=""
|
||||
ItemsSource="{Binding CommonTimeouts}"
|
||||
Text="{Binding Config.IOTimeout, 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>
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace SettingModule.Views.Dialogs
|
||||
{
|
||||
/// <summary>
|
||||
/// VisaConfigView.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class VisaConfigView : UserControl
|
||||
{
|
||||
public VisaConfigView()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user