Compare commits

...

10 Commits

Author SHA1 Message Date
hsc
02d9923474 设备编辑管理界面 2026-06-12 16:55:47 +08:00
hsc
ffb22e1f20 设备编辑1 2026-06-12 09:25:40 +08:00
hsc
a62b6cbc8f 设备连接 2026-06-11 17:25:59 +08:00
hsc
a9ee9e974e 优化设置界面Tcp显示 2026-06-11 16:09:47 +08:00
hsc
5cac253cb8 DIspose添加 2026-06-11 15:45:29 +08:00
hsc
9c661200b9 依赖注入顺序修改 2026-06-11 10:42:00 +08:00
hsc
420ca0ffd6 设备列表 2026-06-11 08:49:07 +08:00
hsc
2e07c0c446 automapper框架优化 2026-06-10 16:05:35 +08:00
hsc
5452857299 设备初始化 2026-06-10 14:54:45 +08:00
hsc
59d047d8e6 默认文件路径保存 2026-06-10 13:08:52 +08:00
82 changed files with 3388 additions and 419 deletions

View File

@@ -1,7 +1,7 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35527.113 d17.12
VisualStudioVersion = 17.12.35527.113
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ADP", "ADP\ADP.csproj", "{8B48B0CD-55F9-4623-9A10-BFE25B21EBD6}"
EndProject
@@ -37,6 +37,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonitorModule", "MonitorMod
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CAN驱动", "CAN驱动\CAN驱动.csproj", "{D1868672-0132-4B1A-A393-C2CF49A25E74}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeviceEditModule", "DeviceEditModule\DeviceEditModule.csproj", "{170AD4C1-189D-4FBE-B10D-2A4304527834}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -107,6 +109,10 @@ Global
{D1868672-0132-4B1A-A393-C2CF49A25E74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1868672-0132-4B1A-A393-C2CF49A25E74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1868672-0132-4B1A-A393-C2CF49A25E74}.Release|Any CPU.Build.0 = Release|Any CPU
{170AD4C1-189D-4FBE-B10D-2A4304527834}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{170AD4C1-189D-4FBE-B10D-2A4304527834}.Debug|Any CPU.Build.0 = Debug|Any CPU
{170AD4C1-189D-4FBE-B10D-2A4304527834}.Release|Any CPU.ActiveCfg = Release|Any CPU
{170AD4C1-189D-4FBE-B10D-2A4304527834}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -118,5 +124,6 @@ Global
{DFC084AE-FE45-4492-A2DA-EAE2941F26E9} = {1E92D601-68FC-45F6-8251-BD0F39226E5B}
{3456C3AC-E7FF-45F8-A68F-BEED9969812C} = {1E92D601-68FC-45F6-8251-BD0F39226E5B}
{850A5BE8-0BC1-4D6A-8351-DD812C9C4427} = {1E92D601-68FC-45F6-8251-BD0F39226E5B}
{170AD4C1-189D-4FBE-B10D-2A4304527834} = {1E92D601-68FC-45F6-8251-BD0F39226E5B}
EndGlobalSection
EndGlobal

View File

@@ -8,10 +8,15 @@
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="16.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CAN驱动\CAN驱动.csproj" />
<ProjectReference Include="..\Command\Command.csproj" />
<ProjectReference Include="..\DeviceCommand\DeviceCommand.csproj" />
<ProjectReference Include="..\DeviceEditModule\DeviceEditModule.csproj" />
<ProjectReference Include="..\LoginModule\LoginModule.csproj" />
<ProjectReference Include="..\MainModule\MainModule.csproj" />
<ProjectReference Include="..\MonitorModule\MonitorModule.csproj" />

View File

@@ -9,6 +9,7 @@
<ResourceDictionary.MergedDictionaries>
<!--自定义style-->
<ResourceDictionary Source="/UIShare;component/Styles/CommonStyle.xaml"></ResourceDictionary>
<ResourceDictionary Source="/UIShare;component/Styles/WindowStyle.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

View File

@@ -17,6 +17,12 @@ using UIShare.PubEvent;
using static System.Runtime.InteropServices.JavaScript.JSType;
using UIShare.GlobalVariable;
using UIShare;
using System;
using DeviceCommand.Device;
using DeviceCommand.Base;
using AutoMapper;
using Microsoft.Extensions.Logging.Abstractions;
using ADP.Profiles;
namespace ADP
{
@@ -65,6 +71,22 @@ namespace ADP
RegionManager.SetRegionManager(Application.Current.MainWindow, re);
login.Show();
}
protected override void RegisterRequiredTypes(IContainerRegistry containerRegistry)
{
base.RegisterRequiredTypes(containerRegistry);
//注册全局变量
containerRegistry.RegisterScoped<SystemConfig>();
containerRegistry.RegisterScoped<StepRunning>();
containerRegistry.RegisterScoped<ScopedContext>();
containerRegistry.RegisterScoped<DeviceManager>();
containerRegistry.RegisterSingleton<GlobalInfo>();
//注册AutoMapper
var config = new MapperConfiguration(
cfg => cfg.AddProfile<AutoMapperProfile>(),
NullLoggerFactory.Instance
);
containerRegistry.RegisterSingleton<IMapper>(() => config.CreateMapper());
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
//注册弹窗
@@ -72,11 +94,8 @@ namespace ADP
// 注册通知管理器
INotificationManager NotificationManager = new NotificationManager();
containerRegistry.RegisterInstance<INotificationManager>(NotificationManager);
//注册全局变量
containerRegistry.RegisterScoped<SystemConfig>();
containerRegistry.RegisterScoped<StepRunning>();
containerRegistry.RegisterScoped<ScopedContext>();
containerRegistry.RegisterSingleton<GlobalInfo>();
// 注册仓储
containerRegistry.RegisterScoped(typeof(SqlSugarRepository<>));
}
//指定模块加载方式(需要手动将模块生成的dll放入Modules文件夹中)
protected override IModuleCatalog CreateModuleCatalog()

View File

@@ -0,0 +1,64 @@
using AutoMapper;
using Model.Models;
using System;
using UIShare.UIViewModel;
namespace ADP.Profiles
{
/// <summary>
/// UIShare.UIViewModel ↔ Model.Models 双向映射配置。
/// </summary>
public class AutoMapperProfile : Profile
{
public AutoMapperProfile()
{
// ===== Parameter =====
// ParameterModel.Type(System.Type) ↔ Parameter.TypeName(string)
// ParameterModel.Category(enum) ↔ Parameter.Category(string)
CreateMap<ParameterVM, Parameter>()
.ForMember(dest => dest.TypeName,
opt => opt.MapFrom(src => src.Type != null ? src.Type.AssemblyQualifiedName : null))
.ForMember(dest => dest.Category,
opt => opt.MapFrom(src => src.Category.ToString()));
CreateMap<Parameter, ParameterVM>()
.ForMember(dest => dest.Type,
opt => opt.MapFrom(src => string.IsNullOrEmpty(src.TypeName)
? typeof(string)
: (Type.GetType(src.TypeName) ?? typeof(string))))
.ForMember(dest => dest.Category,
opt => opt.MapFrom(src => ParseCategory(src.Category)));
// ===== Step =====
CreateMap<StepVM, Step>().ReverseMap();
// ===== Program =====
CreateMap<ProgramVM, Program>().ReverseMap();
// ===== Method =====
CreateMap<MethodVM, Method>().ReverseMap();
// ===== DeviceInfo =====
CreateMap<DeviceInfoVM, DeviceInfo>().ReverseMap();
// ===== CanMessageShow =====
CreateMap<CanMessageShowVM, CanMessageShow>().ReverseMap();
// ===== InstructionNode =====
CreateMap<InstructionNodeVM, InstructionNode>().ReverseMap();
// ===== SubProgramItem =====
CreateMap<SubProgramItemVM,SubProgramItem>().ReverseMap();
}
private static ParameterVM.ParameterCategory ParseCategory(string? category)
{
if (string.IsNullOrEmpty(category))
return ParameterVM.ParameterCategory.Temp;
return Enum.TryParse<ParameterVM.ParameterCategory>(category, true, out var result)
? result
: ParameterVM.ParameterCategory.Temp;
}
}
}

View File

@@ -84,7 +84,8 @@ namespace ADP.ViewModels
_globalInfo.ContextDic.TryGetValue(_globalInfo.CurrentScope, out var ctx) ? ctx : null;
private StepRunning? CurrentRunner =>
_globalInfo.StepRunningDic.TryGetValue(_globalInfo.CurrentScope, out var runner) ? runner : null;
private SystemConfig? CurrentConfig =>
_globalInfo.ConfigDic.TryGetValue(_globalInfo.CurrentScope, out var config) ? config : null;
public string RunState
{
get => CurrentContext?.RunState ?? "运行";
@@ -143,6 +144,7 @@ namespace ADP.ViewModels
public ICommand OpenCommand { get; set; }
public ICommand NewCommand { get; set; }
public ICommand SetDefaultCommand { get; set; }
public ICommand ShowDialogManagerViewCommand { get; set; }
#endregion
public ShellViewModel(IContainerProvider containerProvider)
@@ -153,8 +155,8 @@ namespace ADP.ViewModels
_regionManager = containerProvider.Resolve<IRegionManager>();
_notificationManager = containerProvider.Resolve<INotificationManager>();
_moduleManager = containerProvider.Resolve<IModuleManager>();
LeftDrawerOpenCommand = new DelegateCommand(LeftDrawerOpen);
ShowDialogManagerViewCommand = new DelegateCommand(ShowDialogManagerView);
MinimizeCommand = new DelegateCommand<Window>(MinimizeWindow);
MaximizeCommand = new DelegateCommand<Window>(MaximizeWindow);
CloseCommand = new DelegateCommand<Window>(CloseWindow);
@@ -179,7 +181,7 @@ namespace ADP.ViewModels
Application.Current.MainWindow.Show();
_regionManager.RequestNavigate("ShellViewManager", "MainView");
});
_eventAggregator.GetEvent<RunSingalCompletedEvent>().Subscribe(UpdateRunIcon);
_globalInfo.ScopeChanged += (s, e) =>
@@ -193,6 +195,8 @@ namespace ADP.ViewModels
_uiRefreshTimer.Start();
}
private void RefreshAllContextProperties()
{
RaisePropertyChanged(nameof(RunState));
@@ -203,6 +207,10 @@ namespace ADP.ViewModels
}
#region
private void ShowDialogManagerView()
{
_eventAggregator.GetEvent<CancelMinimizeEvent>().Publish();
}
private async void OnRunning()
{
string runningScope = _globalInfo.CurrentScope;
@@ -459,6 +467,7 @@ namespace ADP.ViewModels
private void SetDefault()
{
if (CurrentConfig == null) return;
// 💡 1. 抓取触发瞬间的 Scope 与 Context 快照
string runningScope = _globalInfo.CurrentScope;
ScopedContext? targetContext = CurrentContext;
@@ -467,8 +476,8 @@ namespace ADP.ViewModels
if (targetContext.CurrentFilePath != null)
{
//SystemConfig.DefaultProgramFilePath = targetContext.CurrentFilePath;
//ConfigService.Save();
CurrentConfig.DefaultProgramFilePath = targetContext.CurrentFilePath;
ConfigService.Save(CurrentConfig);
LoggerHelper.SuccessWithNotify($"工位 [{runningScope}] 已成功将当前程序设为默认启动程序");
}
}
@@ -528,8 +537,8 @@ namespace ADP.ViewModels
// 读取 JSON 文件
string json = File.ReadAllText(filePath);
// 反序列化为 ProgramModel
var program = Newtonsoft.Json.JsonConvert.DeserializeObject<ProgramModel>(json);
// 反序列化为 ProgramVM
var program = Newtonsoft.Json.JsonConvert.DeserializeObject<ProgramVM>(json);
if (program == null)
{
@@ -619,12 +628,12 @@ namespace ADP.ViewModels
LoggerHelper.SuccessWithNotify($"工位 [{runningScope}] 程序保存成功!");
}
// 💡 辅助方法:建议将你的通用序列化落盘代码调整为接收 ProgramModel 参数,提高复用性
private void SaveProgramToFile(string filePath, ProgramModel programModel)
// 💡 辅助方法:建议将你的通用序列化落盘代码调整为接收 ProgramVM 参数,提高复用性
private void SaveProgramToFile(string filePath, ProgramVM ProgramVM)
{
try
{
string json = Newtonsoft.Json.JsonConvert.SerializeObject(programModel, Newtonsoft.Json.Formatting.Indented);
string json = Newtonsoft.Json.JsonConvert.SerializeObject(ProgramVM, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(filePath, json);
}
catch (Exception ex)
@@ -676,10 +685,6 @@ namespace ADP.ViewModels
}
}
_globalInfo.ContextDic.Remove(targetRegionName);
_globalInfo.StepRunningDic.Remove(targetRegionName);
_globalInfo.ScopeDic.Remove(targetRegionName);
var parameters = new NavigationParameters { { "Name", targetRegionName } };
_regionManager.RequestNavigate(targetRegionName, "ProtocolStartView", parameters);
_eventAggregator.GetEvent<ExpandViewEvent>().Publish("");

View File

@@ -49,7 +49,7 @@
Command="{Binding NavigateCommand}"
CommandParameter="{Binding Content, RelativeSource={RelativeSource Self}}"
Style="{StaticResource MaterialDesignFlatButton}"
Margin="8" />
Margin="8" />
<Button Content="记录界面"
Command="{Binding NavigateCommand}"
CommandParameter="{Binding Content, RelativeSource={RelativeSource Self}}"
@@ -59,12 +59,13 @@
Command="{Binding NavigateCommand}"
CommandParameter="{Binding Content, RelativeSource={RelativeSource Self}}"
Style="{StaticResource MaterialDesignFlatButton}"
Margin="8" />
<Button Content="更新界面"
Margin="8" />
<!--<Button Content="更新界面"
Command="{Binding NavigateCommand}"
CommandParameter="{Binding Content, RelativeSource={RelativeSource Self}}"
Style="{StaticResource MaterialDesignFlatButton}"
Margin="8" />
Margin="8" />-->
</StackPanel>
</materialDesign:DrawerHost.LeftDrawerContent>
<Grid>
@@ -96,7 +97,7 @@
Foreground="White" />
</MenuItem.Icon>
</MenuItem>
<!-- 工具菜单 -->
<MenuItem Header="工具"
@@ -159,6 +160,16 @@
</MenuItem>
<!-- 蜂鸣器消音 -->
<MenuItem Header="弹窗管理器"
FontSize="14"
Height="50"
Foreground="Black"
Command="{Binding ShowDialogManagerViewCommand}">
<MenuItem.Icon>
<materialDesign:PackIcon Kind="DialogueOutline"
Foreground="Black" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="蜂鸣器消音"
FontSize="14"
Height="50"
@@ -233,7 +244,7 @@
Foreground="White" />
</MenuItem.Icon>
</MenuItem>
<MenuItem FontSize="14"
Height="50"
Foreground="White">
@@ -303,21 +314,25 @@
Identifier="Root">
<!-- 主内容区 -->
<Grid>
<ContentControl prism:RegionManager.RegionName="ShellViewManager" />
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="31*"/>
</Grid.ColumnDefinitions>
<ContentControl prism:RegionManager.RegionName="ShellViewManager" Grid.ColumnSpan="2" />
<Border x:Name="Overlay"
Background="#40000000"
Visibility="Collapsed"
Panel.ZIndex="1">
Panel.ZIndex="1" Grid.ColumnSpan="2">
<StackPanel Width="150"
VerticalAlignment="Center"
Margin="0 0 0 100">
</StackPanel>
</Border>
<Border x:Name="Waitinglay"
Background="#40000000"
Visibility="Collapsed"
Panel.ZIndex="1">
Panel.ZIndex="1" Grid.ColumnSpan="2">
<StackPanel Width="150"
VerticalAlignment="Center"
Margin="0 0 0 100">

View File

@@ -1,4 +1,5 @@
using System;
using Model.Models;
using System;
using System.IO.Ports;
using System.Text;
using System.Threading;
@@ -25,6 +26,15 @@ namespace DeviceCommand.Base
_serialPort = new SerialPort();
}
/// <summary>
/// 通过 <see cref="SerialPortConfig"/> 一次性配置串口通信参数。
/// </summary>
public Serial_Port(SerialPortConfig config) : this()
{
if (config == null) return;
ConfigureDevice(config.PortName, config.BaudRate, config.DataBits, config.StopBits, config.Parity, config.ReadTimeout, config.WriteTimeout);
}
public void ConfigureDevice(string portName, int baudRate, int dataBits = 8, StopBits stopBits = StopBits.One, Parity parity = Parity.None, int readTimeout = 3000, int writeTimeout = 3000)
{
PortName = portName;

View File

@@ -1,4 +1,5 @@
using System;
using Model.Models;
using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
@@ -23,6 +24,15 @@ namespace DeviceCommand.Base
_tcpClient = new TcpClient();
}
/// <summary>
/// 通过 <see cref="TcpConfig"/> 一次性配置 TCP 通信参数。
/// </summary>
public Tcp(TcpConfig config) : this()
{
if (config == null) return;
ConfigureDevice(config.IPAddress, config.Port, config.SendTimeout, config.ReceiveTimeout);
}
public void ConfigureDevice(string ipAddress, int port, int sendTimeout = 3000, int receiveTimeout = 3000)
{
IPAddress = ipAddress;

View File

@@ -13,6 +13,7 @@
<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,48 @@
using Common.Attributes;
using DeviceCommand.Base;
using NModbus;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DeviceCommand.Device
{
[ADPCommand]
public class IOBoard : ModbusTcp
{
public IOBoard(string Ip地址, int , int , int )
{
ConfigureDevice(Ip地址, , , );
}
public async Task (byte , ushort , bool data)
{
await Modbus.WriteSingleCoilAsync(, , data);
}
public async Task (byte , ushort , bool[] datas)
{
var = await Modbus.ReadCoilsAsync(1, 0, 16);
for (int i = 0; i < datas.Length; i++)
{
if ( + i < .Length)
{
[ + i] = datas[i];
}
}
await Modbus.WriteMultipleCoilsAsync(, 0, );
}
public async Task (byte , ushort )
{
await Modbus.ReadCoilsAsync(, , 1);
}
public async Task<bool[]> (byte , ushort ,ushort )
{
return await Modbus.ReadCoilsAsync(, , );
}
}
}

View File

@@ -1,5 +1,6 @@
using Common.Attributes;
using DeviceCommand.Base;
using Model.Models;
using System;
using System.Globalization;
using System.Threading;
@@ -14,11 +15,10 @@ namespace DeviceCommand.Device
private const string ScpiDelimiter = "\n";
/// <summary>
/// 构造函数:初始化 IT7800E 交直流电源通信参数
/// 构造函数:传入 <see cref="TcpConfig"/> 一次性初始化 IT7800E 交直流电源通信参数
/// </summary>
public IT7800E(string ipAddress, int port, int sendTimeout, int receiveTimeout)
public IT7800E(TcpConfig config) : base(config)
{
ConfigureDevice(ipAddress, port, sendTimeout, receiveTimeout);
}
#region 1. IEEE 488.2

View File

@@ -1,5 +1,6 @@
using Common.Attributes;
using DeviceCommand.Base;
using Model.Models;
using System;
using System.Globalization;
using System.Threading;
@@ -14,11 +15,10 @@ namespace DeviceCommand.Device
private const string ScpiDelimiter = "\n";
/// <summary>
/// 构造函数:初始化 N36200/N36300 设备通信参数
/// 构造函数:传入 <see cref="TcpConfig"/> 一次性初始化 N36200/N36300 设备通信参数
/// </summary>
public N36200(string ipAddress, int port, int sendTimeout, int receiveTimeout)
public N36200(TcpConfig config) : base(config)
{
ConfigureDevice(ipAddress, port, sendTimeout, receiveTimeout);
}
#region 3.1. IEEE 488.2

View File

@@ -1,5 +1,6 @@
using Common.Attributes;
using DeviceCommand.Base;
using Model.Models;
using System;
using System.ComponentModel;
using System.Globalization;
@@ -22,9 +23,8 @@ namespace DeviceCommand.Device
// 手册第 4 页明确规定:每条命令后面都要加结束符 0x0A (\n)
private const string SCPIDelimiter = "\n";
public N36600(string ipAddress, int port, int sendTimeout, int receiveTimeout)
public N36600(TcpConfig config) : base(config)
{
ConfigureDevice(ipAddress, port, sendTimeout, receiveTimeout);
}
/// <summary>

View File

@@ -1,5 +1,6 @@
using Common.Attributes;
using DeviceCommand.Base;
using Model.Models;
using System;
using System.Globalization;
using System.Threading;
@@ -14,11 +15,10 @@ namespace DeviceCommand.Device
private const string ScpiDelimiter = "\n";
/// <summary>
/// 构造函数:初始化 N69200 电子负载通信参数
/// 构造函数:传入 <see cref="TcpConfig"/> 一次性初始化 N69200 电子负载通信参数
/// </summary>
public N69200(string ipAddress, int port, int sendTimeout, int receiveTimeout)
public N69200(TcpConfig config) : base(config)
{
ConfigureDevice(ipAddress, port, sendTimeout, receiveTimeout);
}
#region 3.1. IEEE 488.2

View File

@@ -1,5 +1,6 @@
using Common.Attributes;
using DeviceCommand.Base;
using Model.Models;
using System;
using System.Globalization;
using System.Threading;
@@ -14,11 +15,11 @@ namespace DeviceCommand.Device
private const string ScpiDelimiter = "\n";
/// <summary>
/// 构造函数:初始化示波器通信参数 (鼎阳示波器网口 Socket 默认端口通常为 5025)
/// 构造函数:传入 <see cref="TcpConfig"/> 一次性初始化示波器通信参数。
/// 鼎阳示波器网口 Socket 默认端口通常为 5025请在配置中设置。
/// </summary>
public SDS2000X_HD(string ipAddress, int port = 5025, int sendTimeout = 3000, int receiveTimeout = 3000)
public SDS2000X_HD(TcpConfig config) : base(config)
{
ConfigureDevice(ipAddress, port, sendTimeout, receiveTimeout);
}
#region 1. IEEE 488.2

View File

@@ -1,5 +1,6 @@
using Common.Attributes;
using DeviceCommand.Base;
using Model.Models;
using System;
using System.Globalization;
using System.Threading;
@@ -14,11 +15,10 @@ namespace DeviceCommand.Device
private const string ScpiDelimiter = "\n";
/// <summary>
/// 构造函数:初始化 SPAW7000 功率分析记录仪通信参数
/// 构造函数:传入 <see cref="TcpConfig"/> 一次性初始化 SPAW7000 功率分析记录仪通信参数
/// </summary>
public SPAW7000(string ipAddress, int port, int sendTimeout, int receiveTimeout)
public SPAW7000(TcpConfig config) : base(config)
{
ConfigureDevice(ipAddress, port, sendTimeout, receiveTimeout);
}
#region 1. IEEE 488.2

View File

@@ -0,0 +1,22 @@
using DeviceEditModule.ViewModels;
using DeviceEditModule.Views;
namespace DeviceEditModule
{
public class DeviceEditModule : IModule
{
public void OnInitialized(IContainerProvider containerProvider)
{
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
// DialogMangerView 作为弹窗宿主,注册为 Dialog支持 _dialogService.Show
containerRegistry.RegisterDialog<DialogMangerView, DialogMangerViewModel>("DialogMangerView");
// 设备编辑 View 注册为 Navigation被动态解析后作为 Tab 内容嵌入 DialogMangerView
containerRegistry.RegisterForNavigation<IT7800EView>("IT7800EView");
containerRegistry.RegisterForNavigation<N36200View>("N36200View");
}
}
}

View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include=".\..\UIShare\UIShare.csproj" />
<ProjectReference Include=".\..\DeviceCommand\DeviceCommand.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,135 @@
using Prism.Commands;
using Prism.Ioc;
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using UIShare.PubEvent;
using UIShare.UIViewModel;
using UIShare.ViewModelBase;
namespace DeviceEditModule.ViewModels
{
/// <summary>
/// 弹窗管理器 ViewModel。
/// <para>
/// 职责:<br/>
/// 1. 订阅 <see cref="AddDialogTabEvent"/>,将外部弹窗注册为 Tab<br/>
/// 2. 维护 <see cref="TagItems"/> 集合,控制选中/关闭逻辑;<br/>
/// 3. 最小化:通过 <see cref="MinimizeRequested"/> 事件通知 View 执行 P/Invoke 窗口操作;<br/>
/// 4. 关闭:调用 <see cref="RequestClose"/>。
/// </para>
/// </summary>
public class DialogMangerViewModel : DialogViewModelBase, IDisposable
{
#region
/// <summary>所有 Tab 项集合,绑定到标签条的 ItemsControl。</summary>
public ObservableCollection<DialogTabItemVM> TagItems { get; } = new();
private DialogTabItemVM? _selectedTag;
/// <summary>当前激活的 Tab其 Content 显示在内容区。</summary>
public DialogTabItemVM? SelectedTag
{
get => _selectedTag;
set => SetProperty(ref _selectedTag, value);
}
/// <summary>是否没有任何 Tab用于绑定空状态提示的 Visibility。</summary>
public bool HasNoTabs => TagItems.Count == 0;
#endregion
#region
public ICommand MinimizeCommand { get; }
public ICommand CloseCommand { get; }
#endregion
#region
/// <summary>
/// View 订阅此事件后,在事件回调里执行 P/Invoke 最小化。
/// 这样 ViewModel 无需引用任何 UI 或 Win32 类型。
/// </summary>
public event EventHandler? MinimizeRequested;
public event EventHandler? RestoreRequested;
#endregion
public DialogMangerViewModel(IContainerProvider containerProvider) : base(containerProvider)
{
MinimizeCommand = new DelegateCommand(OnMinimize);
CloseCommand = new DelegateCommand(OnClose);
// 订阅来自其他模块的"添加 Tab"事件
_eventAggregator.GetEvent<AddDialogTabEvent>().Subscribe(OnAddTab, ThreadOption.UIThread);
_eventAggregator.GetEvent<CancelMinimizeEvent>().Subscribe(()=>RestoreRequested.Invoke(this, EventArgs.Empty));
}
#region
private void OnMinimize() => MinimizeRequested?.Invoke(this, EventArgs.Empty);
private void OnClose()
{
// 关闭前清空所有 Tab释放内容引用
TagItems.Clear();
SelectedTag = null;
RequestClose.Invoke();
}
#endregion
#region Tab
/// <summary>收到事件:创建 Tab 并追加到集合,自动切换选中。</summary>
private void OnAddTab(DialogTabInfo info)
{
var tab = new DialogTabItemVM(OnSelectTab, OnCloseTab)
{
Title = info.Title,
Content = info.Content
};
TagItems.Add(tab);
RaisePropertyChanged(nameof(HasNoTabs));
ActivateTab(tab);
}
/// <summary>激活(选中)指定 Tab其余全部取消选中。</summary>
private void OnSelectTab(DialogTabItemVM tab) => ActivateTab(tab);
/// <summary>关闭指定 Tab自动选中相邻 Tab或清空内容区。</summary>
private void OnCloseTab(DialogTabItemVM tab)
{
int idx = TagItems.IndexOf(tab);
TagItems.Remove(tab);
RaisePropertyChanged(nameof(HasNoTabs));
if (TagItems.Count == 0)
{
SelectedTag = null;
return;
}
// 优先选左侧邻 Tab无左侧则选当前索引已向左移一位
ActivateTab(TagItems[Math.Max(0, idx - 1)]);
}
private void ActivateTab(DialogTabItemVM tab)
{
foreach (var t in TagItems)
t.IsSelected = false;
tab.IsSelected = true;
SelectedTag = tab;
}
#endregion
public void Dispose()
{
_eventAggregator.GetEvent<AddDialogTabEvent>().Unsubscribe(OnAddTab);
}
}
}

View File

@@ -0,0 +1,60 @@
using Prism.Commands;
using Prism.Mvvm;
using System;
namespace DeviceEditModule.ViewModels
{
/// <summary>
/// 弹窗管理器中单个 Tab 项的 ViewModel。
/// 由 <see cref="DialogMangerViewModel"/> 在收到 AddDialogTabEvent 时创建,
/// 通过构造函数注入选中/关闭的回调,保持与父 ViewModel 的低耦合。
/// </summary>
public class DialogTabItemVM : BindableBase
{
#region
private string _title = string.Empty;
/// <summary>Tab 标签页上显示的标题。</summary>
public string Title
{
get => _title;
set => SetProperty(ref _title, value);
}
private object? _content;
/// <summary>Tab 内容区域(通常为 UserControl 实例)。</summary>
public object? Content
{
get => _content;
set => SetProperty(ref _content, value);
}
private bool _isSelected;
/// <summary>是否为当前激活 Tab用于切换选中态样式。</summary>
public bool IsSelected
{
get => _isSelected;
set => SetProperty(ref _isSelected, value);
}
#endregion
#region
/// <summary>点击 Tab 标题激活该 Tab。</summary>
public DelegateCommand SelectCommand { get; }
/// <summary>点击 Tab 上的 × 关闭并移除该 Tab。</summary>
public DelegateCommand CloseCommand { get; }
#endregion
/// <param name="onSelect">父 VM 提供的激活回调。</param>
/// <param name="onClose">父 VM 提供的关闭回调。</param>
public DialogTabItemVM(Action<DialogTabItemVM> onSelect, Action<DialogTabItemVM> onClose)
{
SelectCommand = new DelegateCommand(() => onSelect(this));
CloseCommand = new DelegateCommand(() => onClose(this));
}
}
}

View File

@@ -0,0 +1,310 @@
using DeviceCommand.Device;
using Prism.Commands;
using Prism.Ioc;
using System;
using System.Threading;
using System.Windows.Input;
using UIShare.GlobalVariable;
using UIShare.ViewModelBase;
namespace DeviceEditModule.ViewModels
{
/// <summary>
/// IT7800E 交直流电源控制面板 ViewModel。
/// <para>
/// 注册为 Navigation View既可由 Region 导航进入,
/// 也可由外部直接实例化后作为 Tab 内容塞入 DialogMangerView
/// <code>
/// var view = container.Resolve&lt;IT7800EView&gt;();
/// (view.DataContext as IT7800EViewModel)?.Initialize("IT7800E");
/// _eventAggregator.GetEvent&lt;AddDialogTabEvent&gt;().Publish(
/// new DialogTabInfo { Title = "IT7800E", Content = view });
/// </code>
/// </para>
/// </summary>
public class IT7800EViewModel : NavigateViewModelBase, IDisposable
{
#region
private readonly DeviceManager _deviceManager;
private IT7800E? _device;
private CancellationTokenSource? _cts;
#endregion
#region
private string _deviceName = "IT7800E";
public string DeviceName
{
get => _deviceName;
set => SetProperty(ref _deviceName, value);
}
private bool _isConnected;
public bool IsConnected
{
get => _isConnected;
set => SetProperty(ref _isConnected, value);
}
private bool _isBusy;
/// <summary>正在执行设备命令时为 true用于 UI 忙碌状态指示。</summary>
public bool IsBusy
{
get => _isBusy;
set => SetProperty(ref _isBusy, value);
}
#endregion
#region
private double _acVoltage = 220.0;
/// <summary>待设置的交流电压值V。</summary>
public double AcVoltage
{
get => _acVoltage;
set => SetProperty(ref _acVoltage, value);
}
private double _dcVoltage = 0.0;
/// <summary>待设置的直流偏置电压值V。</summary>
public double DcVoltage
{
get => _dcVoltage;
set => SetProperty(ref _dcVoltage, value);
}
private double _frequency = 50.0;
/// <summary>待设置的交流频率Hz。</summary>
public double Frequency
{
get => _frequency;
set => SetProperty(ref _frequency, value);
}
private double _currentLimit = 10.0;
/// <summary>待设置的限流值A。</summary>
public double CurrentLimit
{
get => _currentLimit;
set => SetProperty(ref _currentLimit, value);
}
private string _selectedMode = "AC";
/// <summary>待设置的电源工作模式AC / DC / ACDC。</summary>
public string SelectedMode
{
get => _selectedMode;
set => SetProperty(ref _selectedMode, value);
}
private double _ovpValue = 260.0;
/// <summary>过压保护值V。</summary>
public double OvpValue
{
get => _ovpValue;
set => SetProperty(ref _ovpValue, value);
}
private double _ocpValue = 15.0;
/// <summary>过流保护值A。</summary>
public double OcpValue
{
get => _ocpValue;
set => SetProperty(ref _ocpValue, value);
}
#endregion
#region
private string _measuredVoltage = "—";
public string MeasuredVoltage
{
get => _measuredVoltage;
set => SetProperty(ref _measuredVoltage, value);
}
private string _measuredCurrent = "—";
public string MeasuredCurrent
{
get => _measuredCurrent;
set => SetProperty(ref _measuredCurrent, value);
}
private string _measuredPower = "—";
public string MeasuredPower
{
get => _measuredPower;
set => SetProperty(ref _measuredPower, value);
}
private string _measuredFrequency = "—";
public string MeasuredFrequency
{
get => _measuredFrequency;
set => SetProperty(ref _measuredFrequency, value);
}
private string _responseLog = string.Empty;
/// <summary>命令响应日志(最新消息在顶部)。</summary>
public string ResponseLog
{
get => _responseLog;
set => SetProperty(ref _responseLog, value);
}
#endregion
#region
public ICommand QueryIdentityCommand { get; }
public ICommand ResetDeviceCommand { get; }
public ICommand OutputOnCommand { get; }
public ICommand OutputOffCommand { get; }
public ICommand SetModeCommand { get; }
public ICommand SetAcVoltageCommand { get; }
public ICommand SetDcVoltageCommand { get; }
public ICommand SetFrequencyCommand { get; }
public ICommand SetCurrentCommand { get; }
public ICommand QueryAllMeasureCommand { get; }
public ICommand SetRemoteModeCommand { get; }
public ICommand SetLocalModeCommand { get; }
public ICommand SetOvpCommand { get; }
public ICommand SetOcpCommand { get; }
public ICommand ClearAlarmCommand { get; }
public ICommand ClearErrorCommand { get; }
#endregion
public IT7800EViewModel(IContainerProvider containerProvider) : base(containerProvider)
{
_deviceManager = containerProvider.Resolve<DeviceManager>();
QueryIdentityCommand = new DelegateCommand(async () => await Exec(async () => AppendLog("IDN: " + await _device!.(Ct()))));
ResetDeviceCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.(Ct()); AppendLog("设备已重置"); }));
OutputOnCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.DC输出(true, Ct()); AppendLog("输出已开启"); }));
OutputOffCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.DC输出(false, Ct()); AppendLog("输出已关闭"); }));
SetModeCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.(SelectedMode, Ct()); AppendLog($"模式已设为 {SelectedMode}"); }));
SetAcVoltageCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.(AcVoltage, Ct()); AppendLog($"AC电压已设为 {AcVoltage} V"); }));
SetDcVoltageCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.(DcVoltage, Ct()); AppendLog($"DC偏置已设为 {DcVoltage} V"); }));
SetFrequencyCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.(Frequency, Ct()); AppendLog($"频率已设为 {Frequency} Hz"); }));
SetCurrentCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.(CurrentLimit, Ct()); AppendLog($"限流已设为 {CurrentLimit} A"); }));
SetOvpCommand = new DelegateCommand(async () => await Exec(async () => { await _device!._OVP(OvpValue, Ct()); AppendLog($"OVP已设为 {OvpValue} V"); }));
SetOcpCommand = new DelegateCommand(async () => await Exec(async () => { await _device!._OCP(OcpValue, Ct()); AppendLog($"OCP已设为 {OcpValue} A"); }));
SetRemoteModeCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.(Ct()); AppendLog("已切换到远程控制模式"); }));
SetLocalModeCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.(Ct()); AppendLog("已切换到本地控制模式"); }));
ClearAlarmCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.(Ct()); AppendLog("保护告警已清除"); }));
ClearErrorCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.(Ct()); AppendLog("错误队列已清除"); }));
QueryAllMeasureCommand = new DelegateCommand(async () => await Exec(async () =>
{
MeasuredVoltage = await _device!.(Ct());
MeasuredCurrent = await _device!.(Ct());
MeasuredPower = await _device!.(Ct());
MeasuredFrequency = await _device!.(Ct());
AppendLog($"测量 → 电压:{MeasuredVoltage}V 电流:{MeasuredCurrent}A 功率:{MeasuredPower}W 频率:{MeasuredFrequency}Hz");
}));
Initialize();
}
#region / Navigation
/// <summary>
/// 从 DeviceManager 中查找 IT7800E 设备实例。
/// 优先按 <paramref name="deviceName"/> 查找,否则取第一个匹配类型的设备。
/// </summary>
public void Initialize(string? deviceName = null)
{
IT7800E? found = null;
string? foundName = null;
if (deviceName != null &&
_deviceManager.DeviceMap.TryGetValue(deviceName, out var d) &&
d is IT7800E e)
{
found = e;
foundName = deviceName;
}
else
{
foreach (var kv in _deviceManager.DeviceMap)
{
if (kv.Value is IT7800E it)
{
found = it;
foundName = kv.Key;
break;
}
}
}
_device = found;
DeviceName = foundName ?? "IT7800E (未找到)";
IsConnected = _device?.IsConnected ?? false;
AppendLog(found != null
? $"已关联设备 [{DeviceName}],连接状态:{(IsConnected ? "" : "")}"
: "未在 DeviceManager 中找到 IT7800E 设备,请先初始化设备配置。");
}
public override void OnNavigatedTo(NavigationContext context)
{
var name = context.Parameters.GetValue<string?>("DeviceName");
Initialize(name);
}
#endregion
#region
private CancellationToken Ct() => (_cts = new CancellationTokenSource(TimeSpan.FromSeconds(10))).Token;
private async Task Exec(Func<Task> action)
{
if (_device == null)
{
AppendLog("错误:未关联到设备实例,请检查设备配置。");
return;
}
if (IsBusy) return;
IsBusy = true;
try
{
await action();
IsConnected = _device.IsConnected;
}
catch (OperationCanceledException)
{
AppendLog("命令超时或已取消。");
}
catch (Exception ex)
{
AppendLog($"错误:{ex.Message}");
}
finally
{
IsBusy = false;
}
}
private void AppendLog(string message)
{
var line = $"[{DateTime.Now:HH:mm:ss}] {message}";
ResponseLog = ResponseLog.Length > 4000
? line + "\n" + ResponseLog[..3000]
: line + "\n" + ResponseLog;
}
#endregion
public void Dispose()
{
_cts?.Cancel();
_cts?.Dispose();
}
}
}

View File

@@ -0,0 +1,309 @@
using DeviceCommand.Device;
using Prism.Commands;
using Prism.Ioc;
using System;
using System.Threading;
using System.Windows.Input;
using UIShare.GlobalVariable;
using UIShare.ViewModelBase;
namespace DeviceEditModule.ViewModels
{
/// <summary>
/// N36200 宽范围可编程直流电源控制面板 ViewModel。
/// <para>
/// 注册为 Navigation View可通过 EventAggregator 添加为 DialogMangerView 中的 Tab
/// <code>
/// var view = container.Resolve&lt;N36200View&gt;();
/// (view.DataContext as N36200ViewModel)?.Initialize("N36200");
/// _eventAggregator.GetEvent&lt;AddDialogTabEvent&gt;().Publish(
/// new DialogTabInfo { Title = "N36200", Content = view });
/// </code>
/// </para>
/// </summary>
public class N36200ViewModel : NavigateViewModelBase, IDisposable
{
#region
private readonly DeviceManager _deviceManager;
private N36200? _device;
private CancellationTokenSource? _cts;
#endregion
#region
private string _deviceName = "N36200";
public string DeviceName
{
get => _deviceName;
set => SetProperty(ref _deviceName, value);
}
private bool _isConnected;
public bool IsConnected
{
get => _isConnected;
set => SetProperty(ref _isConnected, value);
}
private bool _isBusy;
public bool IsBusy
{
get => _isBusy;
set => SetProperty(ref _isBusy, value);
}
#endregion
#region
private double _voltage = 12.0;
/// <summary>待设置的输出电压值V。</summary>
public double Voltage
{
get => _voltage;
set => SetProperty(ref _voltage, value);
}
private double _currentLimit = 5.0;
/// <summary>待设置的限流值A。</summary>
public double CurrentLimit
{
get => _currentLimit;
set => SetProperty(ref _currentLimit, value);
}
private string _selectedMode = "NORMal";
/// <summary>待设置的运行模式NORMal / CHARge / SEQuence / CPOWer / CARWave / APG。</summary>
public string SelectedMode
{
get => _selectedMode;
set => SetProperty(ref _selectedMode, value);
}
private double _ovpValue = 15.0;
/// <summary>过压保护值V。</summary>
public double OvpValue
{
get => _ovpValue;
set => SetProperty(ref _ovpValue, value);
}
private double _ocpValue = 6.0;
/// <summary>过流保护值A。</summary>
public double OcpValue
{
get => _ocpValue;
set => SetProperty(ref _ocpValue, value);
}
private double _uvpValue = 0.0;
/// <summary>欠压保护值V。</summary>
public double UvpValue
{
get => _uvpValue;
set => SetProperty(ref _uvpValue, value);
}
private double _oppValue = 100.0;
/// <summary>过功率保护值W。</summary>
public double OppValue
{
get => _oppValue;
set => SetProperty(ref _oppValue, value);
}
#endregion
#region
private string _measuredVoltage = "—";
public string MeasuredVoltage
{
get => _measuredVoltage;
set => SetProperty(ref _measuredVoltage, value);
}
private string _measuredCurrent = "—";
public string MeasuredCurrent
{
get => _measuredCurrent;
set => SetProperty(ref _measuredCurrent, value);
}
private string _measuredPower = "—";
public string MeasuredPower
{
get => _measuredPower;
set => SetProperty(ref _measuredPower, value);
}
private string _deviceStatus = "—";
/// <summary>设备状态字OUTPut:STATe? 查询结果)。</summary>
public string DeviceStatus
{
get => _deviceStatus;
set => SetProperty(ref _deviceStatus, value);
}
private string _responseLog = string.Empty;
/// <summary>命令响应日志(最新消息在顶部)。</summary>
public string ResponseLog
{
get => _responseLog;
set => SetProperty(ref _responseLog, value);
}
#endregion
#region
public ICommand QueryIdentityCommand { get; }
public ICommand ResetDeviceCommand { get; }
public ICommand OutputOnCommand { get; }
public ICommand OutputOffCommand { get; }
public ICommand SetVoltageCommand { get; }
public ICommand SetCurrentCommand { get; }
public ICommand SetModeCommand { get; }
public ICommand QueryAllMeasureCommand { get; }
public ICommand QueryStatusCommand { get; }
public ICommand SetOvpCommand { get; }
public ICommand SetOcpCommand { get; }
public ICommand SetUvpCommand { get; }
public ICommand SetOppCommand { get; }
public ICommand ClearAlarmCommand { get; }
#endregion
public N36200ViewModel(IContainerProvider containerProvider) : base(containerProvider)
{
_deviceManager = containerProvider.Resolve<DeviceManager>();
QueryIdentityCommand = new DelegateCommand(async () => await Exec(async () => AppendLog("IDN: " + await _device!.(Ct()))));
ResetDeviceCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.(Ct()); AppendLog("设备已重置耗时约10s"); }));
OutputOnCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.DC输出(true, Ct()); AppendLog("输出已开启"); }));
OutputOffCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.DC输出(false, Ct()); AppendLog("输出已关闭"); }));
SetVoltageCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.(Voltage, Ct()); AppendLog($"电压已设为 {Voltage} V"); }));
SetCurrentCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.(CurrentLimit, Ct()); AppendLog($"限流已设为 {CurrentLimit} A"); }));
SetModeCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.(SelectedMode, Ct()); AppendLog($"模式已设为 {SelectedMode}"); }));
SetOvpCommand = new DelegateCommand(async () => await Exec(async () => { await _device!._OVP(OvpValue, Ct()); AppendLog($"OVP已设为 {OvpValue} V"); }));
SetOcpCommand = new DelegateCommand(async () => await Exec(async () => { await _device!._OCP(OcpValue, Ct()); AppendLog($"OCP已设为 {OcpValue} A"); }));
SetUvpCommand = new DelegateCommand(async () => await Exec(async () => { await _device!._UVP(UvpValue, Ct()); AppendLog($"UVP已设为 {UvpValue} V"); }));
SetOppCommand = new DelegateCommand(async () => await Exec(async () => { await _device!._OPP(OppValue, Ct()); AppendLog($"OPP已设为 {OppValue} W"); }));
ClearAlarmCommand = new DelegateCommand(async () => await Exec(async () => { await _device!.(Ct()); AppendLog("告警已清除"); }));
QueryStatusCommand = new DelegateCommand(async () => await Exec(async () =>
{
DeviceStatus = await _device!.(Ct());
AppendLog($"状态字: {DeviceStatus}");
}));
QueryAllMeasureCommand = new DelegateCommand(async () => await Exec(async () =>
{
MeasuredVoltage = await _device!.(Ct());
MeasuredCurrent = await _device!.(Ct());
MeasuredPower = await _device!.(Ct());
AppendLog($"测量 → 电压:{MeasuredVoltage}V 电流:{MeasuredCurrent}A 功率:{MeasuredPower}W");
}));
Initialize();
}
#region / Navigation
/// <summary>
/// 从 DeviceManager 中查找 N36200 设备实例。
/// 优先按 <paramref name="deviceName"/> 查找,否则取第一个匹配类型的设备。
/// </summary>
public void Initialize(string? deviceName = null)
{
N36200? found = null;
string? foundName = null;
if (deviceName != null &&
_deviceManager.DeviceMap.TryGetValue(deviceName, out var d) &&
d is N36200 n)
{
found = n;
foundName = deviceName;
}
else
{
foreach (var kv in _deviceManager.DeviceMap)
{
if (kv.Value is N36200 n36)
{
found = n36;
foundName = kv.Key;
break;
}
}
}
_device = found;
DeviceName = foundName ?? "N36200 (未找到)";
IsConnected = _device?.IsConnected ?? false;
AppendLog(found != null
? $"已关联设备 [{DeviceName}],连接状态:{(IsConnected ? "" : "")}"
: "未在 DeviceManager 中找到 N36200 设备,请先初始化设备配置。");
}
public override void OnNavigatedTo(NavigationContext context)
{
var name = context.Parameters.GetValue<string?>("DeviceName");
Initialize(name);
}
#endregion
#region
private CancellationToken Ct() => (_cts = new CancellationTokenSource(TimeSpan.FromSeconds(10))).Token;
private async Task Exec(Func<Task> action)
{
if (_device == null)
{
AppendLog("错误:未关联到设备实例,请检查设备配置。");
return;
}
if (IsBusy) return;
IsBusy = true;
try
{
await action();
IsConnected = _device.IsConnected;
}
catch (OperationCanceledException)
{
AppendLog("命令超时或已取消。");
}
catch (Exception ex)
{
AppendLog($"错误:{ex.Message}");
}
finally
{
IsBusy = false;
}
}
private void AppendLog(string message)
{
var line = $"[{DateTime.Now:HH:mm:ss}] {message}";
ResponseLog = ResponseLog.Length > 4000
? line + "\n" + ResponseLog[..3000]
: line + "\n" + ResponseLog;
}
#endregion
public void Dispose()
{
_cts?.Cancel();
_cts?.Dispose();
}
}
}

View File

@@ -0,0 +1,284 @@
<UserControl x:Class="DeviceEditModule.Views.DialogMangerView"
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:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:prism="http://prismlibrary.com/"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:converters="clr-namespace:UIShare.Converters;assembly=UIShare"
mc:Ignorable="d"
prism:ViewModelLocator.AutoWireViewModel="True"
Height="850" Width="900">
<!-- 此窗口独立开启 ShowInTaskbar以支持最小化后在任务栏恢复 -->
<prism:Dialog.WindowStyle>
<Style TargetType="Window">
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="ResizeMode" Value="NoResize"/>
<Setter Property="ShowInTaskbar" Value="True"/>
<Setter Property="AllowsTransparency" Value="True"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="SizeToContent" Value="WidthAndHeight"/>
</Style>
</prism:Dialog.WindowStyle>
<UserControl.Resources>
<converters:BooleanToVisibilityConverter x:Key="BoolToVis"/>
<!-- ── 窗口拖拽阴影外框 ── -->
<DropShadowEffect x:Key="WindowShadow"
Color="#66000000"
BlurRadius="16"
ShadowDepth="4"
Direction="315"/>
<!-- ── Tab 项:未选中样式 ── -->
<Style x:Key="TabItemStyle" TargetType="Border">
<Setter Property="Background" Value="#F0F0F0"/>
<Setter Property="BorderBrush" Value="#BDBDBD"/>
<Setter Property="BorderThickness" Value="1,1,1,0"/>
<Setter Property="CornerRadius" Value="6,6,0,0"/>
<Setter Property="Margin" Value="2,5,0,0"/>
<Setter Property="Cursor" Value="Hand"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected}" Value="True">
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="#1976D2"/>
<Setter Property="BorderThickness" Value="1,2,1,0"/>
</DataTrigger>
</Style.Triggers>
</Style>
<!-- ── 标题栏按钮 ── -->
<Style x:Key="TitleBarButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="Width" Value="36"/>
<Setter Property="Height" Value="28"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="bg"
Background="{TemplateBinding Background}"
CornerRadius="4">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="bg" Property="Background" Value="#33FFFFFF"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="bg" Property="Background" Value="#55FFFFFF"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- ── Tab 关闭按钮(小号,无边框) ── -->
<Style x:Key="TabCloseButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="#757575"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Width" Value="18"/>
<Setter Property="Height" Value="18"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="bg"
Background="{TemplateBinding Background}"
CornerRadius="9">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="bg" Property="Background" Value="#FFD32F2F"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<!-- 最外层:透明底 + 阴影 + 圆角 -->
<Border CornerRadius="10"
Background="White"
Effect="{StaticResource WindowShadow}"
Margin="8">
<Grid>
<Grid.RowDefinitions>
<!-- 标题栏 -->
<RowDefinition Height="38"/>
<!-- Tab 标签条 -->
<RowDefinition Height="40"/>
<!-- 内容区 -->
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Grid.Row="0"
CornerRadius="10,10,0,0"
Background="#1565C0"
MouseLeftButtonDown="MouseLeftButtonDown">
<Grid Margin="12,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<!-- 图标 + 标题 -->
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<materialDesign:PackIcon Kind="WindowRestore"
Foreground="White"
Width="18" Height="18"
Margin="0,0,6,0"
VerticalAlignment="Center"/>
<TextBlock Text="弹窗管理器"
Foreground="White"
FontSize="13"
FontWeight="Medium"
VerticalAlignment="Center"/>
<!-- Tab 数量徽标 -->
<Border Background="#33FFFFFF"
CornerRadius="10"
Padding="6,1"
Margin="8,0,0,0"
VerticalAlignment="Center">
<TextBlock Foreground="White"
FontSize="11">
<TextBlock.Text>
<Binding Path="TagItems.Count"
StringFormat="{}{0} 个窗口"/>
</TextBlock.Text>
</TextBlock>
</Border>
</StackPanel>
<!-- 最小化按钮 -->
<Button Grid.Column="1"
Content="—"
ToolTip="最小化"
Command="{Binding MinimizeCommand}"
Style="{StaticResource TitleBarButtonStyle}"
Margin="0,0,2,0"/>
<!-- 关闭按钮 -->
<Button Grid.Column="2"
Content="✕"
ToolTip="关闭"
Command="{Binding CloseCommand}"
Style="{StaticResource TitleBarButtonStyle}"/>
</Grid>
</Border>
<Border Grid.Row="1"
Background="#FAFAFA"
BorderBrush="#E0E0E0"
BorderThickness="0,0,0,1">
<ScrollViewer HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Hidden"
Padding="4,0,4,0">
<ItemsControl ItemsSource="{Binding TagItems}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Bottom"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- 单个 Tab -->
<Border Style="{StaticResource TabItemStyle}">
<StackPanel Orientation="Horizontal"
Margin="8,4,4,4"
VerticalAlignment="Center">
<!-- Tab 标题(点击激活) -->
<Button Command="{Binding SelectCommand}"
Background="Transparent"
BorderBrush="Transparent"
Cursor="Hand"
Padding="0"
MaxWidth="130">
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter/>
</ControlTemplate>
</Button.Template>
<TextBlock Text="{Binding Title}"
FontSize="12"
TextTrimming="CharacterEllipsis"
ToolTip="{Binding Title}"
VerticalAlignment="Center">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="#616161"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected}" Value="True">
<Setter Property="Foreground" Value="#1565C0"/>
<Setter Property="FontWeight" Value="SemiBold"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Button>
<!-- 关闭按钮 -->
<Button Command="{Binding CloseCommand}"
Content="✕"
Style="{StaticResource TabCloseButtonStyle}"
Margin="4,0,0,0"
ToolTip="关闭此窗口"/>
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Border>
<Grid Grid.Row="2">
<!-- 空状态提示 -->
<StackPanel HorizontalAlignment="Center"
VerticalAlignment="Center"
Visibility="{Binding HasNoTabs, Converter={StaticResource BoolToVis}}">
<materialDesign:PackIcon Kind="InboxOutline"
Width="64" Height="64"
Foreground="#BDBDBD"
HorizontalAlignment="Center"/>
<TextBlock Text="暂无打开的弹窗"
Foreground="#9E9E9E"
FontSize="14"
Margin="0,8,0,0"
HorizontalAlignment="Center"/>
<TextBlock Text="其他窗口最小化后将在此处显示为标签页"
Foreground="#BDBDBD"
FontSize="11"
Margin="0,4,0,0"
HorizontalAlignment="Center"/>
</StackPanel>
<!-- 选中 Tab 的内容 -->
<ContentControl Content="{Binding SelectedTag.Content}"
Visibility="{Binding HasNoTabs,
Converter={StaticResource BoolToVis},
ConverterParameter=Invert}"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"/>
</Grid>
</Grid>
</Border>
</UserControl>

View File

@@ -0,0 +1,81 @@
using DeviceEditModule.ViewModels;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
namespace DeviceEditModule.Views
{
/// <summary>
/// DialogMangerView 代码后置。
/// 负责处理与 Window 相关的操作最小化ViewModel 通过事件通知,
/// 此处执行 P/Invoke绕过 WPF 在 AllowsTransparency=True 时禁止
/// 直接设置 WindowState.Minimized 的限制。
/// </summary>
public partial class DialogMangerView : UserControl
{
#region P/Invoke
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
/// <summary>SW_MINIMIZE最小化到任务栏。</summary>
private const int SW_MINIMIZE = 6;
/// <summary>SW_RESTORE激活并显示窗口。如果窗口最小化或最大化系统会将其还原到原始大小和位置。</summary>
private const int SW_RESTORE = 9;
#endregion
public DialogMangerView()
{
InitializeComponent();
// 在 Loaded 后订阅 ViewModel 的最小化请求事件
Loaded += (_, _) =>
{
if (DataContext is DialogMangerViewModel vm)
{
vm.MinimizeRequested += OnMinimizeRequested;
vm.RestoreRequested += OnRestoreRequested;
}
};
// 在 Unloaded 时取消订阅,防止内存泄漏
Unloaded += (_, _) =>
{
if (DataContext is DialogMangerViewModel vm)
{
vm.MinimizeRequested -= OnMinimizeRequested;
vm.RestoreRequested -= OnRestoreRequested;
}
};
}
private void OnRestoreRequested(object? sender, EventArgs e)
{
var window = Window.GetWindow(this);
if (window == null) return;
var hwnd = new WindowInteropHelper(window).EnsureHandle();
// 使用 Win32 API 强行恢复窗口,绕过 WPF 的 AllowsTransparency 限制
ShowWindow(hwnd, SW_RESTORE);
}
private void OnMinimizeRequested(object? sender, System.EventArgs e)
{
var window = Window.GetWindow(this);
if (window == null) return;
var hwnd = new WindowInteropHelper(window).EnsureHandle();
ShowWindow(hwnd, SW_MINIMIZE);
}
/// <summary>标题栏拖拽移动窗口。</summary>
private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
Window.GetWindow(this)?.DragMove();
}
}
}

View File

@@ -0,0 +1,318 @@
<UserControl x:Class="DeviceEditModule.Views.IT7800EView"
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:prism="http://prismlibrary.com/"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:converters="clr-namespace:UIShare.Converters;assembly=UIShare"
mc:Ignorable="d"
prism:ViewModelLocator.AutoWireViewModel="True"
d:DesignHeight="760" d:DesignWidth="860">
<UserControl.Resources>
<converters:BooleanToVisibilityConverter x:Key="BoolToVis"/>
<!-- 普通操作按钮 -->
<Style x:Key="CmdBtn" TargetType="Button" BasedOn="{StaticResource MaterialDesignRaisedButton}">
<Setter Property="Height" Value="32"/>
<Setter Property="Padding" Value="12,0"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Margin" Value="4,0"/>
</Style>
<!-- 危险/警告操作按钮(橙色/红色) -->
<Style x:Key="WarnBtn" TargetType="Button" BasedOn="{StaticResource MaterialDesignRaisedButton}">
<Setter Property="Height" Value="32"/>
<Setter Property="Padding" Value="12,0"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Margin" Value="4,0"/>
<Setter Property="Background" Value="#EF6C00"/>
<Setter Property="Foreground" Value="White"/>
</Style>
<!-- 数字输入框 -->
<Style x:Key="NumInput" TargetType="TextBox" BasedOn="{StaticResource MaterialDesignOutlinedTextBox}">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="32"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Margin" Value="4,0"/>
</Style>
<!-- 测量值只读显示 -->
<Style x:Key="MeasureBox" TargetType="TextBox" BasedOn="{StaticResource MaterialDesignOutlinedTextBox}">
<Setter Property="Width" Value="110"/>
<Setter Property="Height" Value="32"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Margin" Value="4,0"/>
<Setter Property="Background" Value="#F5F5F5"/>
</Style>
<!-- 标签 -->
<Style x:Key="ParamLabel" TargetType="TextBlock">
<Setter Property="Width" Value="80"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Margin" Value="0,0,4,0"/>
</Style>
</UserControl.Resources>
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
<StackPanel Margin="12">
<!-- ═══ 设备信息头 ═══ -->
<materialDesign:Card Margin="0,0,0,8" Padding="12,8">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<materialDesign:PackIcon Kind="Flash" Width="22" Height="22"
Foreground="#1565C0" Margin="0,0,8,0"
VerticalAlignment="Center"/>
<TextBlock Text="IT7800E 交直流可编程电源"
FontSize="15" FontWeight="Bold"
VerticalAlignment="Center"/>
<TextBlock Text="{Binding DeviceName, StringFormat=' [{0}]'}"
FontSize="13" Foreground="#757575"
VerticalAlignment="Center" Margin="4,0,0,0"/>
</StackPanel>
<!-- 连接状态指示 -->
<StackPanel Grid.Column="2" Orientation="Horizontal" VerticalAlignment="Center">
<Border Width="10" Height="10" CornerRadius="5" Margin="0,0,6,0">
<Border.Style>
<Style TargetType="Border">
<Setter Property="Background" Value="#F44336"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsConnected}" Value="True">
<Setter Property="Background" Value="#4CAF50"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
<TextBlock VerticalAlignment="Center" FontSize="12">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="未连接"/>
<Setter Property="Foreground" Value="#F44336"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsConnected}" Value="True">
<Setter Property="Text" Value="已连接"/>
<Setter Property="Foreground" Value="#4CAF50"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<!-- 忙碌指示 -->
<ProgressBar IsIndeterminate="True" Width="80" Height="4"
Margin="12,0,0,0"
Visibility="{Binding IsBusy, Converter={StaticResource BoolToVis}}"/>
</StackPanel>
</Grid>
</materialDesign:Card>
<!-- ═══ 主体 2 列 ═══ -->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!-- ─── 左列 ─── -->
<StackPanel Grid.Column="0" Margin="0,0,4,0">
<!-- 输出控制 -->
<GroupBox Header="输出控制" Margin="0,0,0,8"
materialDesign:ColorZoneAssist.Mode="PrimaryLight">
<StackPanel Margin="4,4,4,4">
<!-- 开关输出 -->
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="主输出" Style="{StaticResource ParamLabel}"/>
<Button Content="开启输出" Command="{Binding OutputOnCommand}"
Style="{StaticResource MaterialDesignRaisedButton}"
Background="#388E3C" Foreground="White"
Height="32" Padding="12,0" FontSize="12" Margin="4,0"/>
<Button Content="关闭输出" Command="{Binding OutputOffCommand}"
Style="{StaticResource WarnBtn}"/>
</StackPanel>
<!-- 工作模式 -->
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="工作模式" Style="{StaticResource ParamLabel}"/>
<ComboBox Width="90" Height="32" Margin="4,0"
materialDesign:HintAssist.Hint=""
SelectedItem="{Binding SelectedMode}"
VerticalContentAlignment="Center" FontSize="12">
<ComboBoxItem Content="AC"/>
<ComboBoxItem Content="DC"/>
<ComboBoxItem Content="ACDC"/>
</ComboBox>
<Button Content="设置模式" Command="{Binding SetModeCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
<!-- 远程/本地 -->
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="控制模式" Style="{StaticResource ParamLabel}"/>
<Button Content="远程控制" Command="{Binding SetRemoteModeCommand}"
Style="{StaticResource CmdBtn}"/>
<Button Content="本地控制" Command="{Binding SetLocalModeCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
<!-- 清除 / 重置 -->
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="系统操作" Style="{StaticResource ParamLabel}"/>
<Button Content="清除告警" Command="{Binding ClearAlarmCommand}"
Style="{StaticResource WarnBtn}"/>
<Button Content="清除错误" Command="{Binding ClearErrorCommand}"
Style="{StaticResource WarnBtn}"/>
<Button Content="重置设备" Command="{Binding ResetDeviceCommand}"
Style="{StaticResource WarnBtn}"/>
</StackPanel>
</StackPanel>
</GroupBox>
<!-- 参数设置 -->
<GroupBox Header="参数设置" Margin="0,0,0,8"
materialDesign:ColorZoneAssist.Mode="PrimaryLight">
<StackPanel Margin="4,4,4,4">
<!-- AC 电压 -->
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="AC电压 (V)" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource NumInput}"
materialDesign:HintAssist.Hint=""
Text="{Binding AcVoltage, UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="设置" Command="{Binding SetAcVoltageCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
<!-- DC 偏置 -->
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="DC偏置 (V)" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource NumInput}"
materialDesign:HintAssist.Hint=""
Text="{Binding DcVoltage, UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="设置" Command="{Binding SetDcVoltageCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
<!-- 频率 -->
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="频率 (Hz)" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource NumInput}"
materialDesign:HintAssist.Hint=""
Text="{Binding Frequency, UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="设置" Command="{Binding SetFrequencyCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
<!-- 限流 -->
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="限流 (A)" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource NumInput}"
materialDesign:HintAssist.Hint=""
Text="{Binding CurrentLimit, UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="设置" Command="{Binding SetCurrentCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
</StackPanel>
</GroupBox>
<!-- 保护设置 -->
<GroupBox Header="保护设置" Margin="0,0,0,8"
materialDesign:ColorZoneAssist.Mode="PrimaryLight">
<StackPanel Margin="4,4,4,4">
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="OVP (V)" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource NumInput}"
materialDesign:HintAssist.Hint=""
Text="{Binding OvpValue, UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="设置 OVP" Command="{Binding SetOvpCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="OCP (A)" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource NumInput}"
materialDesign:HintAssist.Hint=""
Text="{Binding OcpValue, UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="设置 OCP" Command="{Binding SetOcpCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
</StackPanel>
</GroupBox>
</StackPanel>
<!-- ─── 右列 ─── -->
<StackPanel Grid.Column="1" Margin="4,0,0,0">
<!-- 实时测量 -->
<GroupBox Header="实时测量" Margin="0,0,0,8"
materialDesign:ColorZoneAssist.Mode="PrimaryLight">
<StackPanel Margin="4,4,4,4">
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="实际电压" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource MeasureBox}"
materialDesign:HintAssist.Hint=""
Text="{Binding MeasuredVoltage, Mode=OneWay}"/>
<TextBlock Text="V" VerticalAlignment="Center" Margin="2,0,8,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="实际电流" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource MeasureBox}"
materialDesign:HintAssist.Hint=""
Text="{Binding MeasuredCurrent, Mode=OneWay}"/>
<TextBlock Text="A" VerticalAlignment="Center" Margin="2,0,8,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="实际功率" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource MeasureBox}"
materialDesign:HintAssist.Hint=""
Text="{Binding MeasuredPower, Mode=OneWay}"/>
<TextBlock Text="W" VerticalAlignment="Center" Margin="2,0,8,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="输出频率" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource MeasureBox}"
materialDesign:HintAssist.Hint=""
Text="{Binding MeasuredFrequency, Mode=OneWay}"/>
<TextBlock Text="Hz" VerticalAlignment="Center" Margin="2,0,8,0"/>
</StackPanel>
<Button Content="刷新全部测量" Command="{Binding QueryAllMeasureCommand}"
Style="{StaticResource CmdBtn}"
HorizontalAlignment="Left" Margin="0,4,0,0"/>
</StackPanel>
</GroupBox>
<!-- 设备信息 -->
<GroupBox Header="设备信息" Margin="0,0,0,8"
materialDesign:ColorZoneAssist.Mode="PrimaryLight">
<StackPanel Orientation="Horizontal" Margin="4,8">
<Button Content="查询 IDN" Command="{Binding QueryIdentityCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
</GroupBox>
<!-- 响应日志 -->
<GroupBox Header="响应日志" Margin="0,0,0,8"
materialDesign:ColorZoneAssist.Mode="PrimaryLight">
<ScrollViewer Height="260" VerticalScrollBarVisibility="Auto">
<TextBox Text="{Binding ResponseLog, Mode=OneWay}"
materialDesign:HintAssist.Hint=""
IsReadOnly="True"
TextWrapping="Wrap"
FontSize="11"
FontFamily="Consolas"
Background="#FAFAFA"
BorderThickness="0"
VerticalAlignment="Top"/>
</ScrollViewer>
</GroupBox>
</StackPanel>
</Grid>
</StackPanel>
</ScrollViewer>
</UserControl>

View 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 DeviceEditModule.Views
{
/// <summary>
/// IT7800EView.xaml 的交互逻辑
/// </summary>
public partial class IT7800EView : UserControl
{
public IT7800EView()
{
InitializeComponent();
}
}
}

View File

@@ -0,0 +1,305 @@
<UserControl x:Class="DeviceEditModule.Views.N36200View"
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:prism="http://prismlibrary.com/"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:converters="clr-namespace:UIShare.Converters;assembly=UIShare"
mc:Ignorable="d"
prism:ViewModelLocator.AutoWireViewModel="True"
d:DesignHeight="760" d:DesignWidth="860">
<UserControl.Resources>
<converters:BooleanToVisibilityConverter x:Key="BoolToVis"/>
<Style x:Key="CmdBtn" TargetType="Button" BasedOn="{StaticResource MaterialDesignRaisedButton}">
<Setter Property="Height" Value="32"/>
<Setter Property="Padding" Value="12,0"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Margin" Value="4,0"/>
</Style>
<Style x:Key="WarnBtn" TargetType="Button" BasedOn="{StaticResource MaterialDesignRaisedButton}">
<Setter Property="Height" Value="32"/>
<Setter Property="Padding" Value="12,0"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Margin" Value="4,0"/>
<Setter Property="Background" Value="#EF6C00"/>
<Setter Property="Foreground" Value="White"/>
</Style>
<Style x:Key="NumInput" TargetType="TextBox" BasedOn="{StaticResource MaterialDesignOutlinedTextBox}">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="32"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Margin" Value="4,0"/>
</Style>
<Style x:Key="MeasureBox" TargetType="TextBox" BasedOn="{StaticResource MaterialDesignOutlinedTextBox}">
<Setter Property="Width" Value="110"/>
<Setter Property="Height" Value="32"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Margin" Value="4,0"/>
<Setter Property="Background" Value="#F5F5F5"/>
</Style>
<Style x:Key="ParamLabel" TargetType="TextBlock">
<Setter Property="Width" Value="80"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Margin" Value="0,0,4,0"/>
</Style>
</UserControl.Resources>
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
<StackPanel Margin="12">
<!-- ═══ 设备信息头 ═══ -->
<materialDesign:Card Margin="0,0,0,8" Padding="12,8">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<materialDesign:PackIcon Kind="BatteryCharging" Width="22" Height="22"
Foreground="#2E7D32" Margin="0,0,8,0"
VerticalAlignment="Center"/>
<TextBlock Text="N36200 宽范围可编程直流电源"
FontSize="15" FontWeight="Bold"
VerticalAlignment="Center"/>
<TextBlock Text="{Binding DeviceName, StringFormat=' [{0}]'}"
FontSize="13" Foreground="#757575"
VerticalAlignment="Center" Margin="4,0,0,0"/>
</StackPanel>
<!-- 连接状态指示 -->
<StackPanel Grid.Column="2" Orientation="Horizontal" VerticalAlignment="Center">
<Border Width="10" Height="10" CornerRadius="5" Margin="0,0,6,0">
<Border.Style>
<Style TargetType="Border">
<Setter Property="Background" Value="#F44336"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsConnected}" Value="True">
<Setter Property="Background" Value="#4CAF50"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
<TextBlock VerticalAlignment="Center" FontSize="12">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="未连接"/>
<Setter Property="Foreground" Value="#F44336"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsConnected}" Value="True">
<Setter Property="Text" Value="已连接"/>
<Setter Property="Foreground" Value="#4CAF50"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<ProgressBar IsIndeterminate="True" Width="80" Height="4"
Margin="12,0,0,0"
Visibility="{Binding IsBusy, Converter={StaticResource BoolToVis}}"/>
</StackPanel>
</Grid>
</materialDesign:Card>
<!-- ═══ 主体 2 列 ═══ -->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!-- ─── 左列 ─── -->
<StackPanel Grid.Column="0" Margin="0,0,4,0">
<!-- 输出控制 -->
<GroupBox Header="输出控制" Margin="0,0,0,8"
materialDesign:ColorZoneAssist.Mode="PrimaryLight">
<StackPanel Margin="4,4,4,4">
<!-- 开关输出 -->
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="DC 输出" Style="{StaticResource ParamLabel}"/>
<Button Content="开启输出" Command="{Binding OutputOnCommand}"
Style="{StaticResource MaterialDesignRaisedButton}"
Background="#388E3C" Foreground="White"
Height="32" Padding="12,0" FontSize="12" Margin="4,0"/>
<Button Content="关闭输出" Command="{Binding OutputOffCommand}"
Style="{StaticResource WarnBtn}"/>
</StackPanel>
<!-- 运行模式 -->
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="运行模式" Style="{StaticResource ParamLabel}"/>
<ComboBox Width="100" Height="32" Margin="4,0"
materialDesign:HintAssist.Hint=""
SelectedItem="{Binding SelectedMode}"
VerticalContentAlignment="Center" FontSize="12">
<ComboBoxItem Content="NORMal"/>
<ComboBoxItem Content="CHARge"/>
<ComboBoxItem Content="SEQuence"/>
<ComboBoxItem Content="CPOWer"/>
<ComboBoxItem Content="CARWave"/>
</ComboBox>
<Button Content="设置" Command="{Binding SetModeCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
<!-- 重置 / 清除 / IDN -->
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="系统操作" Style="{StaticResource ParamLabel}"/>
<Button Content="清除告警" Command="{Binding ClearAlarmCommand}"
Style="{StaticResource WarnBtn}"/>
<Button Content="重置设备" Command="{Binding ResetDeviceCommand}"
Style="{StaticResource WarnBtn}"/>
</StackPanel>
</StackPanel>
</GroupBox>
<!-- 参数设置 -->
<GroupBox Header="参数设置" Margin="0,0,0,8"
materialDesign:ColorZoneAssist.Mode="PrimaryLight">
<StackPanel Margin="4,4,4,4">
<!-- 电压 -->
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="电压 (V)" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource NumInput}"
materialDesign:HintAssist.Hint=""
Text="{Binding Voltage, UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="设置" Command="{Binding SetVoltageCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
<!-- 限流 -->
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="限流 (A)" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource NumInput}"
materialDesign:HintAssist.Hint=""
Text="{Binding CurrentLimit, UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="设置" Command="{Binding SetCurrentCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
</StackPanel>
</GroupBox>
<!-- 保护设置 -->
<GroupBox Header="保护设置" Margin="0,0,0,8"
materialDesign:ColorZoneAssist.Mode="PrimaryLight">
<StackPanel Margin="4,4,4,4">
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="OVP (V)" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource NumInput}"
materialDesign:HintAssist.Hint=""
Text="{Binding OvpValue, UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="设置 OVP" Command="{Binding SetOvpCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="OCP (A)" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource NumInput}"
materialDesign:HintAssist.Hint=""
Text="{Binding OcpValue, UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="设置 OCP" Command="{Binding SetOcpCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="UVP (V)" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource NumInput}"
materialDesign:HintAssist.Hint=""
Text="{Binding UvpValue, UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="设置 UVP" Command="{Binding SetUvpCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="OPP (W)" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource NumInput}"
materialDesign:HintAssist.Hint=""
Text="{Binding OppValue, UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="设置 OPP" Command="{Binding SetOppCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
</StackPanel>
</GroupBox>
</StackPanel>
<!-- ─── 右列 ─── -->
<StackPanel Grid.Column="1" Margin="4,0,0,0">
<!-- 实时测量 -->
<GroupBox Header="实时测量" Margin="0,0,0,8"
materialDesign:ColorZoneAssist.Mode="PrimaryLight">
<StackPanel Margin="4,4,4,4">
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="实际电压" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource MeasureBox}"
materialDesign:HintAssist.Hint=""
Text="{Binding MeasuredVoltage, Mode=OneWay}"/>
<TextBlock Text="V" VerticalAlignment="Center" Margin="2,0,8,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="实际电流" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource MeasureBox}"
materialDesign:HintAssist.Hint=""
Text="{Binding MeasuredCurrent, Mode=OneWay}"/>
<TextBlock Text="A" VerticalAlignment="Center" Margin="2,0,8,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="实际功率" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource MeasureBox}"
materialDesign:HintAssist.Hint=""
Text="{Binding MeasuredPower, Mode=OneWay}"/>
<TextBlock Text="W" VerticalAlignment="Center" Margin="2,0,8,0"/>
</StackPanel>
<Button Content="刷新全部测量" Command="{Binding QueryAllMeasureCommand}"
Style="{StaticResource CmdBtn}"
HorizontalAlignment="Left" Margin="0,4,0,0"/>
</StackPanel>
</GroupBox>
<!-- 设备信息与状态 -->
<GroupBox Header="设备信息与状态" Margin="0,0,0,8"
materialDesign:ColorZoneAssist.Mode="PrimaryLight">
<StackPanel Margin="4,4,4,4">
<StackPanel Orientation="Horizontal" Margin="0,4">
<Button Content="查询 IDN" Command="{Binding QueryIdentityCommand}"
Style="{StaticResource CmdBtn}"/>
<Button Content="查询状态字" Command="{Binding QueryStatusCommand}"
Style="{StaticResource CmdBtn}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,4">
<TextBlock Text="状态字" Style="{StaticResource ParamLabel}"/>
<TextBox Style="{StaticResource MeasureBox}" Width="200"
materialDesign:HintAssist.Hint=""
Text="{Binding DeviceStatus, Mode=OneWay}"/>
</StackPanel>
</StackPanel>
</GroupBox>
<!-- 响应日志 -->
<GroupBox Header="响应日志" Margin="0,0,0,8"
materialDesign:ColorZoneAssist.Mode="PrimaryLight">
<ScrollViewer Height="300" VerticalScrollBarVisibility="Auto">
<TextBox Text="{Binding ResponseLog, Mode=OneWay}"
materialDesign:HintAssist.Hint=""
IsReadOnly="True"
TextWrapping="Wrap"
FontSize="11"
FontFamily="Consolas"
Background="#FAFAFA"
BorderThickness="0"
VerticalAlignment="Top"/>
</ScrollViewer>
</GroupBox>
</StackPanel>
</Grid>
</StackPanel>
</ScrollViewer>
</UserControl>

View 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 DeviceEditModule.Views
{
/// <summary>
/// N36200View.xaml 的交互逻辑
/// </summary>
public partial class N36200View : UserControl
{
public N36200View()
{
InitializeComponent();
}
}
}

View File

@@ -1,3 +1,5 @@
using DeviceCommand.Base;
using DeviceCommand.Device;
using MainModule.Views;
using System.Reflection;
using UIShare.GlobalVariable;
@@ -18,11 +20,6 @@ namespace MainModule
containerRegistry.RegisterForNavigation<MainView>("MainView");
containerRegistry.RegisterForNavigation<AutomatedTestingView>("AutomatedTestingView");
containerRegistry.RegisterForNavigation<ProtocolStartView>("ProtocolStartView");
// Scoped: one ScopedContext per container scope.
// AutomatedTestingViewModel creates its own scope (IContainerExtension.CreateScope)
// and resolves the 5 child VMs from it, so all siblings inside one parent share
// the same ScopedContext, while different parents get isolated instances.
}
}
}

View File

@@ -1,11 +1,15 @@
using System;
using System.IO;
using System.Windows.Input;
using Logger;
using Prism.Ioc;
using TestingModule.ViewModels;
using UIShare;
using UIShare.GlobalVariable;
using UIShare.PubEvent;
using UIShare.UIViewModel;
using UIShare.ViewModelBase;
using static System.Formats.Asn1.AsnWriter;
namespace MainModule.ViewModels
{
@@ -14,6 +18,7 @@ namespace MainModule.ViewModels
#region
private string _testStatus;
private readonly IScopedProvider _scope;
private bool IsInitialized =false;
#endregion
#region
@@ -30,6 +35,7 @@ namespace MainModule.ViewModels
public StepRunning _stepRunning { get; }
public GlobalInfo _globalInfo { get; }
public SystemConfig _systemConfig { get; set; }
public DeviceManager _deviceManager { get; set; }
// 5 个子 ViewModel 全部从同一个 scope 解析,自动注入同一个 ScopedContext
public CommandTreeViewModel CommandTreeVM { get; }
@@ -41,16 +47,29 @@ namespace MainModule.ViewModels
public ICommand RefreshCommand { get; set; }
public ICommand BackToProtocolCommand { get; set; }
public ICommand LoadedCommand { get; set; }
public AutomatedTestingViewModel(IContainerExtension container) : base(container)
{
// 每个 AutomatedTestingViewModel 实例创建独立的容器作用域
_scope = container.CreateScope();
_globalInfo=container.Resolve<GlobalInfo>();
// 在该作用域内解析 ScopedContext —— 当前作用域唯一
_globalInfo =container.Resolve<GlobalInfo>();
_systemConfig = _scope.Resolve<SystemConfig>();
//加载Json数据
if (ConfigService.IsExit(_globalInfo.CurrentOpeningScope))
{
string filePath = System.IO.Path.Combine(_systemConfig.SystemPath, $"{_globalInfo.CurrentOpeningScope}.json");
if (System.IO.File.Exists(filePath))
{
string json = System.IO.File.ReadAllText(filePath);
Newtonsoft.Json.JsonConvert.PopulateObject(json, _systemConfig);
}
}
//容器解析顺序不要改变!!!
_scopedContext = _scope.Resolve<ScopedContext>();
_deviceManager = _scope.Resolve<DeviceManager>();
_stepRunning = _scope.Resolve<StepRunning>();
// 关键:从同一个 _scope 解析 5 个子 VMDI 会把同一个 ScopedContext 注入它们
// 从同一个 _scope 解析 5 个子 VMDI 会把同一个 ScopedContext 注入它们
CommandTreeVM = _scope.Resolve<CommandTreeViewModel>();
StepsManagerVM = _scope.Resolve<StepsManagerViewModel>();
SingleStepEditVM = _scope.Resolve<SingleStepEditViewModel>();
@@ -58,23 +77,40 @@ namespace MainModule.ViewModels
ParametersManagerVM = _scope.Resolve<ParametersManagerViewModel>();
RefreshCommand = new DelegateCommand(OnRefresh);
BackToProtocolCommand = new DelegateCommand(OnBackToProtocol);
LoadedCommand = new AsyncDelegateCommand(OnLoad);
}
public void Dispose()
{
// 1. 如果 TestStatus 为空,说明还没走到 OnNavigatedTo 赋值,直接释放 scope 即可
if (string.IsNullOrEmpty(TestStatus))
{
_scope?.Dispose();
return;
}
try
{
// 2. 显式释放硬件资源(防止端口占用/死锁)
if (_deviceManager is IDisposable disposableDevice)
{
disposableDevice.Dispose();
}
(CommandTreeVM as IDisposable)?.Dispose();
(StepsManagerVM as IDisposable)?.Dispose();
(SingleStepEditVM as IDisposable)?.Dispose();
(LogAreaVM as IDisposable)?.Dispose();
(ParametersManagerVM as IDisposable)?.Dispose();
_globalInfo.ContextDic?.Remove(TestStatus);
_globalInfo.StepRunningDic?.Remove(TestStatus);
_globalInfo.ConfigDic?.Remove(TestStatus);
_globalInfo.ScopeDic?.Remove(TestStatus);
}
catch (Exception ex)
{
Logger.LoggerHelper.ErrorWithNotify($"卸载机台 [{TestStatus}] 全局引用失败: {ex.Message}");
Logger.LoggerHelper.ErrorWithNotify($"卸载机台 [{TestStatus}] 全局引用或资源失败: {ex.Message}");
}
finally
{
@@ -82,6 +118,15 @@ namespace MainModule.ViewModels
}
}
#region
private async Task OnLoad()
{
if (!IsInitialized)
{
await _deviceManager.ConnectAllDevices();
IsInitialized = true;
}
}
private void OnRefresh()
{
// 双击:把自己的名字扔出去
@@ -107,17 +152,27 @@ namespace MainModule.ViewModels
_globalInfo.ContextDic.Add(TestStatus, _scopedContext);
_globalInfo.StepRunningDic.Add(TestStatus, _stepRunning);
_globalInfo.ScopeDic.Add(TestStatus, _scope);
_systemConfig = _scope.Resolve<SystemConfig>();
if (ConfigService.IsExit(TestStatus))
_globalInfo.ConfigDic.Add(TestStatus, _systemConfig);
if(_systemConfig.DefaultProgramFilePath != null&&File.Exists(_systemConfig.DefaultProgramFilePath))
{
string filePath = System.IO.Path.Combine(_systemConfig.SystemPath, $"{TestStatus}.json");
if (System.IO.File.Exists(filePath))
{
string json = System.IO.File.ReadAllText(filePath);
var filePath = _systemConfig.DefaultProgramFilePath;
// 读取 JSON 文件
string json = File.ReadAllText(filePath);
// 🔥 关键:把 json 数据直接灌入当前实例,对象引用没有任何改变
Newtonsoft.Json.JsonConvert.PopulateObject(json, _systemConfig);
// 反序列化为 ProgramVM
var program = Newtonsoft.Json.JsonConvert.DeserializeObject<ProgramVM>(json);
if (program == null)
{
LoggerHelper.WarnWithNotify($"文件格式不正确或为空: {filePath}");
return;
}
// 💡 2. 严格赋值给快照锁定下的当前工位上下文,实现数据完全隔离
_scopedContext.Program.Parameters = program.Parameters;
_scopedContext.Program.StepCollection = program.StepCollection;
_scopedContext.Program.ErrorStepCollection = program.ErrorStepCollection;
_scopedContext.CurrentFilePath = filePath;
}
}

View File

@@ -6,6 +6,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using UIShare.GlobalVariable;
using UIShare.ViewModelBase;
namespace MainModule.ViewModels
@@ -15,6 +16,7 @@ namespace MainModule.ViewModels
#region
private string _testStatus;
private string _moduleColor;
private GlobalInfo _globalInfo;
#endregion
#region
@@ -24,11 +26,7 @@ namespace MainModule.ViewModels
set => SetProperty(ref _testStatus, value);
}
public string ModuleColor
{
get => _moduleColor;
set => SetProperty(ref _moduleColor, value);
}
#endregion
#region
@@ -37,6 +35,7 @@ namespace MainModule.ViewModels
public ProtocolStartViewModel(IContainerProvider containerProvider) : base(containerProvider)
{
_globalInfo = containerProvider.Resolve<GlobalInfo>();
StartProtocolCommand = new DelegateCommand(OnStart);
}
@@ -69,8 +68,7 @@ namespace MainModule.ViewModels
// 2. 透传名称与颜色参数,使 AutomatedTestingViewModel 能正确初始化
var parameters = new NavigationParameters();
parameters.Add("Name", TestStatus);
parameters.Add("Color", ModuleColor);
_globalInfo.CurrentOpeningScope = TestStatus;
// 3. 在本格子 Region 内请求导航,导航成功后再移除旧的 ProtocolStartView 以释放资源
_regionManager.RequestNavigate(regionName, viewName, navResult =>
{
@@ -102,8 +100,7 @@ namespace MainModule.ViewModels
if (navigationContext.Parameters.ContainsKey("Name"))
TestStatus = navigationContext.Parameters.GetValue<string>("Name");
if (navigationContext.Parameters.ContainsKey("Color"))
ModuleColor = navigationContext.Parameters.GetValue<string>("Color");
}
#endregion
}

View File

@@ -15,6 +15,11 @@
<UserControl.Resources>
<converters:LessThanConverter x:Key="LessThanConverter" />
</UserControl.Resources>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding LoadedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Border >
<i:Interaction.Behaviors>
<b:MouseDoubleClickBehavior

View File

@@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="SqlSugarCore" Version="5.1.4.215-preview13" />
<PackageReference Include="System.IO.Ports" Version="11.0.0-preview.4.26230.115" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,24 @@
namespace Model.Models
{
/// <summary>
/// CAN 报文展示纯数据类(对应 UIShare.UIViewModel.CanMessageShowModel
/// </summary>
public class CanMessageShow
{
public byte { get; set; }
public int ID { get; set; }
public ulong { get; set; }
public byte { get; set; }
public bool FD { get; set; }
public bool IsTx { get; set; }
public byte[] Bytes { get; set; } = new byte[64];
public string? { get; set; }
}
}

View File

@@ -0,0 +1,18 @@
namespace Model.Models
{
/// <summary>
/// 设备信息纯数据类(对应 UIShare.UIViewModel.DeviceInfoModel
/// </summary>
public class DeviceInfo
{
public string? DeviceName { get; set; }
public string? DeviceType { get; set; }
public string? Remark { get; set; }
public bool IsEnabled { get; set; }
public bool IsConnected { get; set; }
}
}

View File

@@ -0,0 +1,16 @@
using System.Collections.Generic;
namespace Model.Models
{
/// <summary>
/// 指令节点纯数据类(对应 UIShare.UIViewModel.InstructionNode
/// </summary>
public class InstructionNode
{
public string? Name { get; set; }
public IList<InstructionNode> Children { get; set; } = new List<InstructionNode>();
public object? Tag { get; set; }
}
}

16
Model/Models/Method.cs Normal file
View File

@@ -0,0 +1,16 @@
using System.Collections.Generic;
namespace Model.Models
{
/// <summary>
/// 方法纯数据类(对应 UIShare.UIViewModel.MethodModel
/// </summary>
public class Method
{
public string? Name { get; set; }
public string? FullName { get; set; }
public IList<Parameter> Parameters { get; set; } = new List<Parameter>();
}
}

45
Model/Models/Parameter.cs Normal file
View File

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
namespace Model.Models
{
/// <summary>
/// 参数纯数据类(对应 UIShare.UIViewModel.ParameterModel
/// </summary>
public class Parameter
{
public Guid ID { get; set; } = Guid.NewGuid();
public bool IsVisible { get; set; } = true;
public string? Name { get; set; }
/// <summary>
/// 类型全名(对应 ParameterModel.Type 的 FullName
/// </summary>
public string? TypeName { get; set; }
/// <summary>
/// 参数类别Input / Output / Temp
/// </summary>
public string? Category { get; set; }
public bool IsGlobal { get; set; }
public object? Value { get; set; }
public object? LowerLimit { get; set; }
public object? UpperLimit { get; set; }
public bool Result { get; set; } = true;
public bool IsUseVar { get; set; }
public bool IsSave { get; set; }
public string? VariableName { get; set; }
public Guid? VariableID { get; set; }
}
}

19
Model/Models/Program.cs Normal file
View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
namespace Model.Models
{
/// <summary>
/// 程序纯数据类(对应 UIShare.UIViewModel.ProgramModel
/// </summary>
public class Program
{
public Guid ID { get; set; } = Guid.NewGuid();
public IList<Step> StepCollection { get; set; } = new List<Step>();
public IList<Step> ErrorStepCollection { get; set; } = new List<Step>();
public IList<Parameter> Parameters { get; set; } = new List<Parameter>();
}
}

View File

@@ -0,0 +1,27 @@
using System.IO.Ports;
namespace Model.Models
{
/// <summary>
/// 串口通信参数DeviceCommand 内部纯数据类,供设备类构造函数使用)。
/// 与 UIShare.UIViewModel.SerialPortConfigVM 字段一一对应,
/// StopBits / Parity 在此处使用 System.IO.Ports 强类型枚举,
/// 由 DeviceManager 从字符串解析后填入。
/// </summary>
public class SerialPortConfig
{
public string PortName { get; set; } = "COM1";
public int BaudRate { get; set; } = 9600;
public int DataBits { get; set; } = 8;
public StopBits StopBits { get; set; } = StopBits.One;
public Parity Parity { get; set; } = Parity.None;
public int ReadTimeout { get; set; } = 3000;
public int WriteTimeout { get; set; } = 3000;
}
}

45
Model/Models/Step.cs Normal file
View File

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
namespace Model.Models
{
/// <summary>
/// 步骤纯数据类(对应 UIShare.UIViewModel.StepModel
/// </summary>
public class Step
{
public Guid ID { get; set; } = Guid.NewGuid();
public bool IsUsed { get; set; } = true;
public int Index { get; set; }
public string? Name { get; set; }
public string? StepType { get; set; }
public Method? Method { get; set; }
public Program? SubProgram { get; set; }
public int? LoopCount { get; set; }
public int? CurrentLoopCount { get; set; }
public Guid? LoopStartStepId { get; set; }
public int Result { get; set; } = -1;
public int? RunTime { get; set; }
public string? OKExpression { get; set; }
public string GotoSettingString { get; set; } = "";
public Guid? OKGotoStepID { get; set; }
public Guid? NGGotoStepID { get; set; }
public string? Description { get; set; }
}
}

View File

@@ -0,0 +1,12 @@
namespace Model.Models
{
/// <summary>
/// 子程序项纯数据类(对应 UIShare.UIViewModel.SubProgramItem
/// </summary>
public class SubProgramItem
{
public string Name { get; set; } = "";
public string FilePath { get; set; } = "";
}
}

17
Model/Models/TcpConfig.cs Normal file
View File

@@ -0,0 +1,17 @@
namespace Model.Models
{
/// <summary>
/// TCP 通信参数DeviceCommand 内部纯数据类,供设备类构造函数使用)。
/// 与 UIShare.UIViewModel.TcpConfigVM 字段一一对应,由 DeviceManager 在实例化时填充。
/// </summary>
public class TcpConfig
{
public string IPAddress { get; set; } = "127.0.0.1";
public int Port { get; set; } = 502;
public int SendTimeout { get; set; } = 3000;
public int ReceiveTimeout { get; set; } = 3000;
}
}

View File

@@ -10,7 +10,7 @@ namespace SettingModule.ViewModels.Dialogs
{
/// <summary>
/// 串口连接配置对话框 VM。
/// 通过 DialogParameters 接收宿主 DeviceInfoModel;保存时把副本写回宿主。
/// 通过 DialogParameters 接收宿主 DeviceInfoVM保存时把副本写回宿主。
/// </summary>
public class SerialPortConfigViewModel : DialogViewModelBase
{
@@ -23,8 +23,8 @@ namespace SettingModule.ViewModels.Dialogs
set => SetProperty(ref _title, value);
}
private SerialPortConnectionConfig _config = new();
public SerialPortConnectionConfig Config
private SerialPortConfigVM _config = new();
public SerialPortConfigVM Config
{
get => _config;
set => SetProperty(ref _config, value);
@@ -70,7 +70,7 @@ namespace SettingModule.ViewModels.Dialogs
public ICommand RefreshPortsCommand { get; }
#endregion
private DeviceInfoModel? _hostDevice;
private DeviceInfoVM? _hostDevice;
public SerialPortConfigViewModel(IContainerProvider containerProvider) : base(containerProvider)
{
@@ -126,7 +126,7 @@ namespace SettingModule.ViewModels.Dialogs
if (_hostDevice != null)
{
_hostDevice.SerialPortConfig ??= new SerialPortConnectionConfig();
_hostDevice.SerialPortConfig ??= new SerialPortConfigVM();
Config.CopyTo(_hostDevice.SerialPortConfig);
_hostDevice.ConnectionType = "Serial";
}
@@ -143,13 +143,13 @@ namespace SettingModule.ViewModels.Dialogs
if (parameters.ContainsKey("Device"))
{
_hostDevice = parameters.GetValue<DeviceInfoModel>("Device");
_hostDevice = parameters.GetValue<DeviceInfoVM>("Device");
Title = $"串口连接配置 - {_hostDevice?.DeviceName}";
Config = new SerialPortConnectionConfig(_hostDevice?.SerialPortConfig);
Config = new SerialPortConfigVM(_hostDevice?.SerialPortConfig);
}
else if (parameters.ContainsKey("Config"))
{
Config = new SerialPortConnectionConfig(parameters.GetValue<SerialPortConnectionConfig>("Config"));
Config = new SerialPortConfigVM(parameters.GetValue<SerialPortConfigVM>("Config"));
}
RefreshPorts();

View File

@@ -8,14 +8,14 @@ using UIShare.ViewModelBase;
namespace SettingModule.ViewModels.Dialogs
{
/// <summary>
/// TCP 连接配置对话框 VM。
/// 通过 DialogParameters 接收宿主 DeviceInfoModel;保存时把副本写回宿主。
/// Tcp 连接配置对话框 VM。
/// 通过 DialogParameters 接收宿主 DeviceInfoVM保存时把副本写回宿主。
/// </summary>
public class TCPConfigViewModel : DialogViewModelBase
{
#region
private string _title = "TCP 连接配置";
private string _title = "Tcp 连接配置";
public string Title
{
get => _title;
@@ -23,8 +23,8 @@ namespace SettingModule.ViewModels.Dialogs
}
/// <summary>编辑用的副本,取消时不会污染宿主对象。</summary>
private TcpConnectionConfig _config = new();
public TcpConnectionConfig Config
private TcpConfigVM _config = new();
public TcpConfigVM Config
{
get => _config;
set => SetProperty(ref _config, value);
@@ -57,7 +57,7 @@ namespace SettingModule.ViewModels.Dialogs
#endregion
// 用于保存时把副本回写到原对象
private DeviceInfoModel? _hostDevice;
private DeviceInfoVM? _hostDevice;
public TCPConfigViewModel(IContainerProvider containerProvider) : base(containerProvider)
{
@@ -103,9 +103,9 @@ namespace SettingModule.ViewModels.Dialogs
// 把副本写回宿主
if (_hostDevice != null)
{
_hostDevice.TcpConfig ??= new TcpConnectionConfig();
_hostDevice.TcpConfig ??= new TcpConfigVM();
Config.CopyTo(_hostDevice.TcpConfig);
_hostDevice.ConnectionType = "TCP";
_hostDevice.ConnectionType = "Tcp";
}
RequestClose.Invoke(ButtonResult.OK);
@@ -120,13 +120,13 @@ namespace SettingModule.ViewModels.Dialogs
if (parameters.ContainsKey("Device"))
{
_hostDevice = parameters.GetValue<DeviceInfoModel>("Device");
Title = $"TCP 连接配置 - {_hostDevice?.DeviceName}";
Config = new TcpConnectionConfig(_hostDevice?.TcpConfig);
_hostDevice = parameters.GetValue<DeviceInfoVM>("Device");
Title = $"Tcp 连接配置 - {_hostDevice?.DeviceName}";
Config = new TcpConfigVM(_hostDevice?.TcpConfig);
}
else if (parameters.ContainsKey("Config"))
{
Config = new TcpConnectionConfig(parameters.GetValue<TcpConnectionConfig>("Config"));
Config = new TcpConfigVM(parameters.GetValue<TcpConfigVM>("Config"));
}
}

View File

@@ -15,15 +15,20 @@ namespace SettingModule.ViewModels
{
#region
private SystemConfig _systemConfig;
public SystemConfig SystemConfig
{
get => _systemConfig;
set => SetProperty(ref _systemConfig, value);
}
public bool KeepAlive => true;
public string TestStatus
{
get => _testStatus;
set => SetProperty(ref _testStatus, value);
}
public DeviceInfoModel? SelectedDevice
public DeviceInfoVM? SelectedDevice
{
get => _selectedDevice;
set
@@ -35,7 +40,7 @@ namespace SettingModule.ViewModels
}
}
public ObservableCollection<DeviceInfoModel> DeviceList
public ObservableCollection<DeviceInfoVM> DeviceList
{
get => _deviceList;
set => SetProperty(ref _deviceList, value);
@@ -56,18 +61,17 @@ namespace SettingModule.ViewModels
#region
public ICommand RefreshCommand { get; }
public ICommand SaveCommand { get; }
public ICommand ResetCommand { get; }
public ICommand OpenConnectionConfigCommand { get; }
#endregion
#region
private IScopedProvider _scope;
private SystemConfig _systemConfig;
private ScopedContext _scopedContext { get; set; }
private GlobalInfo _globalInfo { get; }
private bool IsInitiated = false;
private string _testStatus = string.Empty;
private DeviceInfoModel? _selectedDevice;
private ObservableCollection<DeviceInfoModel> _deviceList;
private DeviceInfoVM? _selectedDevice;
private ObservableCollection<DeviceInfoVM> _deviceList;
private string _statusMessage = "请在左侧选择设备查看 / 编辑配置";
#endregion
public SettingViewModel(IContainerExtension container) : base(container)
@@ -75,13 +79,24 @@ namespace SettingModule.ViewModels
_globalInfo = container.Resolve<GlobalInfo>();
RefreshCommand = new DelegateCommand(OnExpand);
SaveCommand = new DelegateCommand(OnSave);
ResetCommand = new DelegateCommand(OnReset);
OpenConnectionConfigCommand = new DelegateCommand(OnOpenConnectionConfig);
}
public void Dispose()
{
_scope?.Dispose();
try
{
if (DeviceList != null)
{
DeviceList = null!;
}
SelectedDevice = null;
_scopedContext = null!;
}
catch (Exception ex)
{
Logger.LoggerHelper.ErrorWithNotify($"释放配置管理组件SettingViewModel资源失败: {ex.Message}");
}
}
#region
@@ -110,35 +125,27 @@ namespace SettingModule.ViewModels
/// <summary>保存当前 SystemConfig 到 SystemPath 下,文件名为 {Title}.json。</summary>
private void OnSave()
{
if (_systemConfig == null)
if (SystemConfig == null)
{
StatusMessage = "无可保存的配置";
return;
}
if (string.IsNullOrWhiteSpace(_systemConfig.Title))
if (string.IsNullOrWhiteSpace(SystemConfig.Title))
{
StatusMessage = "保存失败标题Title不能为空";
return;
}
ConfigService.Save(_systemConfig);
StatusMessage = $"已保存配置 [{_systemConfig.Title}.json] 至 {_systemConfig.SystemPath}{DateTime.Now:HH:mm:ss}";
ConfigService.Save(SystemConfig);
StatusMessage = $"已保存配置 [{SystemConfig.Title}.json] 至 {SystemConfig.SystemPath}{DateTime.Now:HH:mm:ss}";
}
/// <summary>重置当前选中设备的配置(占位)。</summary>
private void OnReset()
{
if (SelectedDevice == null)
{
StatusMessage = "无可重置的设备";
return;
}
StatusMessage = $"已重置设备 [{SelectedDevice.DeviceName}] 的配置";
}
/// <summary>
/// 打开"连接配置"对话框。按 SelectedDevice.ConnectionType 决定开 TCP 还是串口对话框。
/// 打开"连接配置"对话框。按 SelectedDevice.ConnectionType 决定开 Tcp 还是串口对话框。
/// </summary>
private void OnOpenConnectionConfig()
{
@@ -157,14 +164,14 @@ namespace SettingModule.ViewModels
if (string.IsNullOrEmpty(dialogName))
{
StatusMessage = "当前设备未配置连接方式(请先选择 TCP 或 Serial";
StatusMessage = "当前设备未配置连接方式(请先选择 Tcp 或 Serial";
return;
}
var p = new DialogParameters
{
{ "Device", SelectedDevice },
{ "SystemConfig", _systemConfig }
{ "SystemConfig", SystemConfig }
};
_dialogService.ShowDialog(dialogName, p, r =>
@@ -186,15 +193,16 @@ namespace SettingModule.ViewModels
TestStatus = navigationContext.Parameters.GetValue<string>("Name");
_scope = _globalInfo.ScopeDic[TestStatus];
_scopedContext = _globalInfo.ContextDic[TestStatus];
_systemConfig = _scope.Resolve<SystemConfig>();
SystemConfig = _scope.Resolve<SystemConfig>();
if (DeviceList != null && DeviceList.Count > 0)
{
SelectedDevice = DeviceList[0];
}
DeviceList = _systemConfig.DeviceList;
DeviceList = SystemConfig.DeviceList;
IsInitiated = true;
}
}
#endregion
}
}

View File

@@ -205,7 +205,7 @@
<Run Text="{Binding SelectedDevice.DeviceName, FallbackValue=未选中}"/>
</TextBlock>
<StackPanel Grid.Column="1" Orientation="Horizontal">
<Button Content="重置" Command="{Binding ResetCommand}" Padding="12,4"/>
<Button Content="保存" Command="{Binding SaveCommand}" Padding="12,4" Margin="6,0,0,0"/>
</StackPanel>
</Grid>
@@ -310,20 +310,20 @@
<ComboBox materialDesign:HintAssist.Hint="" Grid.Row="0" Grid.Column="1"
Margin="0,4"
ItemsSource="{Binding ConnectionTypes}"
SelectedItem="{Binding SelectedDevice.ConnectionType}"/>
SelectedItem="{Binding SelectedDevice.ConnectionType,Mode=TwoWay}"/>
<Button Grid.Row="0" Grid.Column="2"
Content="配置..."
Margin="8,4,0,4" Padding="14,2"
Command="{Binding OpenConnectionConfigCommand}"/>
<!-- TCP 参数预览 -->
<!-- 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">
<DataTrigger Binding="{Binding SelectedDevice.ConnectionType}" Value="Tcp">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
@@ -414,7 +414,6 @@
VerticalAlignment="Center"
FontWeight="Bold"/>
<StackPanel Grid.Column="1" Orientation="Horizontal">
<Button Content="重置" Command="{Binding ResetCommand}" Padding="12,4"/>
<Button Content="保存" Command="{Binding SaveCommand}" Padding="12,4" Margin="6,0,0,0"/>
</StackPanel>
</Grid>

View File

@@ -18,13 +18,13 @@ using System.Windows.Controls;
using System.Windows.Input;
using System.Xml;
using Model;
using static UIShare.UIViewModel.ParameterModel;
using static UIShare.UIViewModel.ParameterVM;
using UIShare.ViewModelBase;
using NLog;
namespace TestingModule.ViewModels
{
public class CommandTreeViewModel:NavigateViewModelBase
public class CommandTreeViewModel:NavigateViewModelBase,IDisposable
{
#region
private string _SearchText;
@@ -34,14 +34,14 @@ namespace TestingModule.ViewModels
get => _SearchText;
set => SetProperty(ref _SearchText, value);
}
private ObservableCollection<InstructionNode> _instructionTree = new();
public ObservableCollection<InstructionNode> InstructionTree
private ObservableCollection<InstructionNodeVM> _instructionTree = new();
public ObservableCollection<InstructionNodeVM> InstructionTree
{
get => _instructionTree;
set => SetProperty(ref _instructionTree, value);
}
public ProgramModel Program
public ProgramVM Program
{
get => _ScopedContext.Program;
set
@@ -67,15 +67,15 @@ namespace TestingModule.ViewModels
}
}
private ObservableCollection<SubProgramItem> _subPrograms = new();
public ObservableCollection<SubProgramItem> SubPrograms
private ObservableCollection<SubProgramItemVM> _subPrograms = new();
public ObservableCollection<SubProgramItemVM> SubPrograms
{
get => _subPrograms;
set => SetProperty(ref _subPrograms, value);
}
private Dictionary<object, InstructionNode> _treeNodeMap = new();
public Dictionary<object, InstructionNode> TreeNodeMap
private Dictionary<object, InstructionNodeVM> _treeNodeMap = new();
public Dictionary<object, InstructionNodeVM> TreeNodeMap
{
get => _treeNodeMap;
set => SetProperty(ref _treeNodeMap, value);
@@ -108,7 +108,25 @@ namespace TestingModule.ViewModels
ReloadCommand = new DelegateCommand(Reload);
}
public void Dispose()
{
try
{
_ScopedContext = null!;
InstructionTree?.Clear();
SubPrograms?.Clear();
TreeNodeMap?.Clear();
XmlDocumentCache?.Clear();
InstructionTree = null!;
SubPrograms = null!;
TreeNodeMap = null!;
XmlDocumentCache = null!;
}
catch (Exception ex)
{
LoggerHelper.Error($"清理指令树缓存失败: {ex.Message}");
}
}
#region
private void Reload()
{
@@ -119,7 +137,7 @@ namespace TestingModule.ViewModels
private void TreeDoubleClick(object obj)
{
if (!_globalInfo.IsAdmin) return;
if(obj is InstructionNode Node)
if(obj is InstructionNodeVM Node)
{
if(Node.Children.Count == 0)
{
@@ -140,7 +158,7 @@ namespace TestingModule.ViewModels
break;
}
}
else if(Node.Tag is SubProgramItem subProgram)
else if(Node.Tag is SubProgramItemVM subProgram)
{
AddSubProgramToProgram(subProgram, index);
}
@@ -211,7 +229,7 @@ namespace TestingModule.ViewModels
{
try
{
SubPrograms.Add(new SubProgramItem
SubPrograms.Add(new SubProgramItemVM
{
Name = Path.GetFileNameWithoutExtension(filePath),
FilePath = filePath
@@ -229,7 +247,7 @@ namespace TestingModule.ViewModels
private void LoadInstructionsToTreeView()
{
InstructionTree.Clear();
var controlRootNode = new InstructionNode
var controlRootNode = new InstructionNodeVM
{
Name = "系统指令",
Tag = "ControlRoot"
@@ -237,14 +255,14 @@ namespace TestingModule.ViewModels
InstructionTree.Add(controlRootNode);
// 循环开始
controlRootNode.Children.Add(new InstructionNode
controlRootNode.Children.Add(new InstructionNodeVM
{
Name = "循环开始",
Tag = "循环开始"
});
// 循环结束
controlRootNode.Children.Add(new InstructionNode
controlRootNode.Children.Add(new InstructionNodeVM
{
Name = "循环结束",
Tag = "循环结束"
@@ -253,7 +271,7 @@ namespace TestingModule.ViewModels
// ----------------------
// 子程序 根节点
// ----------------------
var subProgramRoot = new InstructionNode
var subProgramRoot = new InstructionNodeVM
{
Name = "子程序",
Tag = "SubProgramRoot"
@@ -262,7 +280,7 @@ namespace TestingModule.ViewModels
foreach (var subProgram in SubPrograms)
{
subProgramRoot.Children.Add(new InstructionNode
subProgramRoot.Children.Add(new InstructionNodeVM
{
Name = subProgram.Name,
Tag = subProgram,
@@ -304,7 +322,7 @@ namespace TestingModule.ViewModels
if (validTypes.Count > 0)
{
var assemblyNode = new InstructionNode
var assemblyNode = new InstructionNodeVM
{
Name = assembly.GetName().Name,
Tag = assembly
@@ -320,7 +338,7 @@ namespace TestingModule.ViewModels
// continue;
//}
var typeNode = new InstructionNode
var typeNode = new InstructionNodeVM
{
Name = type.Name,
Tag = type,
@@ -346,7 +364,7 @@ namespace TestingModule.ViewModels
var parameters = method.GetParameters();
var paramText = string.Join(", ", parameters.Select(p => $"{p.ParameterType.Name} {p.Name}"));
var methodNode = new InstructionNode
var methodNode = new InstructionNodeVM
{
Name = $"{method.Name}({paramText})",
Tag = method,
@@ -422,11 +440,11 @@ namespace TestingModule.ViewModels
{
try
{
var newStep = new StepModel
var newStep = new StepVM
{
Name = method.Name,
StepType = "方法",
Method = new MethodModel
Method = new MethodVM
{
FullName = method.DeclaringType?.FullName,
Name = method.Name
@@ -436,7 +454,7 @@ namespace TestingModule.ViewModels
// 添加输入参数
foreach (var param in method.GetParameters())
{
newStep.Method.Parameters.Add(new ParameterModel
newStep.Method.Parameters.Add(new ParameterVM
{
Name = param.Name!,
Type = param.ParameterType,
@@ -455,7 +473,7 @@ namespace TestingModule.ViewModels
{
// 提取实际返回类型(如 Task<bool> -> bool
Type actualType = returnType.GetGenericArguments()[0];
newStep.Method.Parameters.Add(new ParameterModel
newStep.Method.Parameters.Add(new ParameterVM
{
Name = "Result",
Type = actualType, // 使用实际类型
@@ -465,7 +483,7 @@ namespace TestingModule.ViewModels
else if (returnType != typeof(void))
{
// 同步方法正常添加
newStep.Method.Parameters.Add(new ParameterModel
newStep.Method.Parameters.Add(new ParameterVM
{
Name = "Result",
Type = returnType,
@@ -490,17 +508,17 @@ namespace TestingModule.ViewModels
}
}
private void AddSubProgramToProgram(SubProgramItem subProgram, int insertIndex = -1)
private void AddSubProgramToProgram(SubProgramItemVM subProgram, int insertIndex = -1)
{
try
{
var newStep = new StepModel
var newStep = new StepVM
{
Name = subProgram.Name,
StepType = "子程序"
};
var jsonstr = File.ReadAllText($"{subProgram.FilePath}");
var tmp = JsonConvert.DeserializeObject<ProgramModel>(jsonstr);
var tmp = JsonConvert.DeserializeObject<ProgramVM>(jsonstr);
if (tmp != null)
{
newStep.SubProgram = tmp;
@@ -525,7 +543,7 @@ namespace TestingModule.ViewModels
private void AddLoopStartStep(int insertIndex = -1)
{
var newStep = new StepModel
var newStep = new StepVM
{
Name = "循环开始",
StepType = "循环开始",
@@ -549,7 +567,7 @@ namespace TestingModule.ViewModels
private void AddLoopEndStep(int insertIndex = -1)
{
// 查找最近的未匹配循环开始
StepModel? lastUnmatchedLoopStart = null;
StepVM? lastUnmatchedLoopStart = null;
for (int i = Program.StepCollection.Count - 1; i >= 0; i--)
{
if (Program.StepCollection[i].StepType == "循环开始")
@@ -564,7 +582,7 @@ namespace TestingModule.ViewModels
}
}
var newStep = new StepModel
var newStep = new StepVM
{
Name = "循环结束",
StepType = "循环结束",
@@ -584,6 +602,8 @@ namespace TestingModule.ViewModels
}
}
#endregion
}

View File

@@ -8,7 +8,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using UIShare.GlobalVariable;
using static UIShare.UIViewModel.ParameterModel;
using static UIShare.UIViewModel.ParameterVM;
using UIShare.ViewModelBase;
using Prism.Ioc;
using Prism.Navigation.Regions;
@@ -58,14 +58,14 @@ namespace TestingModule.ViewModels.Dialogs
set => SetProperty(ref _Mode, value);
}
private ProgramModel _program;
public ProgramModel Program
private ProgramVM _program;
public ProgramVM Program
{
get => _program;
set => SetProperty(ref _program, value);
}
private ParameterModel _Parameter;
public ParameterModel Parameter
private ParameterVM _Parameter;
public ParameterVM Parameter
{
get => _Parameter;
set => SetProperty(ref _Parameter, value);
@@ -127,7 +127,7 @@ namespace TestingModule.ViewModels.Dialogs
}
else
{
Parameter = new ParameterModel(_ScopedContext.SelectedParameter);
Parameter = new ParameterVM(_ScopedContext.SelectedParameter);
}
}
#endregion

View File

@@ -11,12 +11,11 @@ using UIShare.ViewModelBase;
namespace TestingModule.ViewModels
{
public class LogAreaViewModel : NavigateViewModelBase
public class LogAreaViewModel : NavigateViewModelBase, IDisposable
{
// 日志集合
private ObservableCollection<LogItem> _logs = new();
public ObservableCollection<LogItem> Logs
{
get => _logs;
@@ -27,18 +26,42 @@ namespace TestingModule.ViewModels
public LogAreaViewModel(IContainerProvider containerProvider) : base(containerProvider)
{
ClearLogCommand = new DelegateCommand(ClearLog);
// 2. 保持原有逻辑,但请确保在 Dispose 中对其进行清理
LoggerHelper.Progress = new System.Progress<(string message, string color, int depth)>(
log =>
{
// 增加防御性代码:防止进入销毁流程时异步回调引发空引用异常
if (Logs == null) return;
var brush = (Brush)new BrushConverter().ConvertFromString(log.color);
Logs.Add(new LogItem(log.message, brush, log.depth));
});
}
private void ClearLog()
{
Logs.Clear();
Logs?.Clear();
}
/// <summary>
/// 完善后的资源释放方法
/// </summary>
public void Dispose()
{
try
{
LoggerHelper.Progress = null!;
if (Logs != null)
{
Logs.Clear();
Logs = null!;
}
}
catch (Exception ex)
{
Logger.LoggerHelper.ErrorWithNotify($"释放日志组件LogAreaViewModel资源失败: {ex.Message}");
}
}
}

View File

@@ -5,6 +5,7 @@ using Logger;
using MaterialDesignThemes.Wpf;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Windows.Input;
using System.Xml;
using UIShare.ViewModelBase;
@@ -12,7 +13,7 @@ using Prism.Events;
namespace TestingModule.ViewModels
{
public class ParametersManagerViewModel:NavigateViewModelBase
public class ParametersManagerViewModel:NavigateViewModelBase, IDisposable
{
#region
//private ObservableCollection<DeviceConfigModel> _DeviceList;
@@ -22,15 +23,15 @@ namespace TestingModule.ViewModels
// get { return _DeviceList; }
// set { SetProperty(ref _DeviceList,value); }
//}
private ObservableCollection<DeviceInfoModel> _DeviceInfoModel;
private ObservableCollection<DeviceInfoVM> _DeviceInfoModel;
public ObservableCollection<DeviceInfoModel> DeviceInfoModel
public ObservableCollection<DeviceInfoVM> DeviceInfoVM
{
get { return _DeviceInfoModel; }
set { SetProperty(ref _DeviceInfoModel, value); }
}
public ProgramModel Program
public ProgramVM Program
{
get => _ScopedContext.Program;
set
@@ -42,8 +43,8 @@ namespace TestingModule.ViewModels
}
}
}
private ParameterModel _SelectedParameter;
public ParameterModel SelectedParameter
private ParameterVM _SelectedParameter;
public ParameterVM SelectedParameter
{
get => _SelectedParameter;
set
@@ -54,11 +55,11 @@ namespace TestingModule.ViewModels
}
}
}
private DeviceInfoModel _SelectedDevice;
private DeviceInfoVM _SelectedDevice;
public DeviceInfoModel SelectedDevice
public DeviceInfoVM SelectedDevice
{
get { return _SelectedDevice; }
set { SetProperty(ref _SelectedDevice, value); }
@@ -67,44 +68,102 @@ namespace TestingModule.ViewModels
private ScopedContext _ScopedContext { get; set; }
private readonly SystemConfig _systemConfig;
private readonly GlobalInfo _globalInfo;
private readonly DeviceManager _deviceManager;
private readonly IContainerProvider _containerProvider;
#region
public ICommand ParameterAddCommand { get; set; }
public ICommand ParameterEditCommand { get; set; }
public ICommand ParameterDeleteCommand { get; set; }
public ICommand DeviceEditCommand { get; set; }
public ICommand ReconnnectCommand { get; set; }
public ICommand ReConnectCommand { get; set; }
public ICommand CloseCommand { get; set; }
public ICommand LoadedCommand { get; set; }
#endregion
public ParametersManagerViewModel(IContainerProvider containerProvider, ScopedContext scopedContext, SystemConfig systemConfig, GlobalInfo globalInfo) : base(containerProvider)
public ParametersManagerViewModel(IContainerProvider containerProvider) : base(containerProvider)
{
_ScopedContext = scopedContext;
_systemConfig = systemConfig;
_globalInfo = globalInfo;
_ScopedContext = containerProvider.Resolve<ScopedContext>();
_systemConfig = containerProvider.Resolve<SystemConfig>();
_deviceManager = containerProvider.Resolve<DeviceManager>();
_globalInfo = containerProvider.Resolve<GlobalInfo>();
_containerProvider = containerProvider;
Program = _ScopedContext.Program;
ParameterAddCommand = new DelegateCommand(ParameterAdd);
ParameterEditCommand = new DelegateCommand(ParameterEdit);
ParameterDeleteCommand = new DelegateCommand(ParameterDelete);
DeviceEditCommand = new DelegateCommand(DeviceEdit);
ReConnectCommand = new AsyncDelegateCommand(OnReConnect);
CloseCommand = new AsyncDelegateCommand(OnClose);
LoadedCommand = new DelegateCommand(OnLoad);
}
#region
private void OnLoad()
{
DeviceInfoVM = _systemConfig.DeviceList;
} private async Task OnReConnect()
{
await _deviceManager.ConnectSpecifiedDevice(SelectedDevice.DeviceName);
} private async Task OnClose()
{
await _deviceManager.CloseDeviceAsync(SelectedDevice.DeviceName);
}
/// <summary>
/// 跟踪已打开的弹窗管理器窗口实例,避免重复打开多个 DialogMangerView 窗口。
/// </summary>
private static System.Windows.Window? _dialogWindow;
private void DeviceEdit()
{
if (!_globalInfo.IsAdmin) return;
if (SelectedDevice==null)
{
return;
}
if (SelectedDevice == null) return;
var type = SelectedDevice.DeviceType.Split('.').Last();
if(type=="E36233A"|| type == "IT6724CReverse")
var viewName = type + "View";
try
{
_dialogService.Show("Backfeed");
// 1. 先确保弹窗管理器窗口已打开(首次 Show后续只追加 Tab
if (_dialogWindow == null || !_dialogWindow.IsVisible)
{
_dialogService.Show("DialogMangerView");
// 找到刚刚被 DialogService 打开的窗口(按 DataContext 类型名匹配)
_dialogWindow = System.Windows.Application.Current.Windows
.OfType<System.Windows.Window>()
.FirstOrDefault(w => w.DataContext?.GetType().Name == "DialogMangerViewModel");
}
// 2. 从容器按注册名解析设备编辑 View
var view = _containerProvider.Resolve<object>(viewName) as System.Windows.FrameworkElement;
if (view == null) return;
// 3. 通过反射调用 ViewModel 上的 Initialize(deviceName) 方法,
// 避免 TestingModule 直接引用 DeviceEditModule 的类型
var vm = view.DataContext;
if (vm != null)
{
var initMethod = vm.GetType().GetMethod("Initialize", new[] { typeof(string) });
initMethod?.Invoke(vm, new object[] { SelectedDevice.DeviceName });
}
// 4. 发布事件 → DialogMangerViewModel 接收后将此 View 添加为 Tab
_eventAggregator.GetEvent<AddDialogTabEvent>().Publish(new DialogTabInfo
{
Title = $"{type} [{SelectedDevice.DeviceName}]",
Content = view
});
// 5. 将窗口置顶(确保用户看到新增的 Tab
if (_dialogWindow != null && _dialogWindow.IsVisible)
{
if (_dialogWindow.WindowState == System.Windows.WindowState.Minimized)
_dialogWindow.WindowState = System.Windows.WindowState.Normal;
_dialogWindow.Activate();
}
}
else
catch (Exception ex)
{
_dialogService.Show(type);
LoggerHelper.ErrorWithNotify($"打开设备编辑窗口 [{type}] 失败:{ex.Message}");
}
}
@@ -160,6 +219,24 @@ namespace TestingModule.ViewModels
#endregion
public void Dispose()
{
try
{
DeviceInfoVM?.Clear();
DeviceInfoVM = null!;
SelectedParameter = null!;
SelectedDevice = null!;
if (_ScopedContext != null)
{
_ScopedContext.SelectedParameter = null;
_ScopedContext = null!;
}
}
catch (Exception ex)
{
Logger.LoggerHelper.Error($"释放参数管理组件ParametersManagerViewModel资源失败: {ex.Message}");
}
}
}
}

View File

@@ -18,7 +18,7 @@ using UIShare.GlobalVariable;
namespace TestingModule.ViewModels
{
public class SingleStepEditViewModel:NavigateViewModelBase
public class SingleStepEditViewModel:NavigateViewModelBase,IDisposable
{
#region
private Guid _ID;
@@ -27,13 +27,13 @@ namespace TestingModule.ViewModels
get => _ID;
set => SetProperty(ref _ID, value);
}
private StepModel _SelectedStep;
public StepModel SelectedStep
private StepVM _SelectedStep;
public StepVM SelectedStep
{
get => _SelectedStep;
set => SetProperty(ref _SelectedStep, value);
}
public ProgramModel Program
public ProgramVM Program
{
get => _ScopedContext.Program;
set
@@ -193,9 +193,29 @@ namespace TestingModule.ViewModels
{
if (_ScopedContext.SelectedStep == null) return;
ID = _ScopedContext.SelectedStep.ID;
SelectedStep = new StepModel(_ScopedContext.SelectedStep);
SelectedStep = new StepVM(_ScopedContext.SelectedStep);
}
#endregion
public void Dispose()
{
try
{
// 1. 【核心修复】必须严格退订所有全局 Prism 事件
_eventAggregator?.GetEvent<EditSetpEvent>()?.Unsubscribe(EditSingleStep);
_eventAggregator?.GetEvent<DeletedStepEvent>()?.Unsubscribe(DisposeSelectedStep);
_eventAggregator?.GetEvent<ParamsChangedEvent>()?.Unsubscribe(ParamsChanged);
// 2. 清空当前正在编辑的步骤副本,断开前台绑定,防止 UI 视图树悬挂
SelectedStep = null!;
// 3. 断开对工位隔离上下文的强引用
_ScopedContext = null!;
}
catch (Exception ex)
{
LoggerHelper.Error($"释放单步编辑组件SingleStepEditViewModel资源失败: {ex.Message}");
}
}
}
}

View File

@@ -17,7 +17,7 @@ using Prism.Events;
namespace TestingModule.ViewModels
{
public class StepsManagerViewModel:NavigateViewModelBase
public class StepsManagerViewModel:NavigateViewModelBase, IDisposable
{
#region
private string _Title;
@@ -44,14 +44,14 @@ namespace TestingModule.ViewModels
get { return _selectedTabHeader; }
set { SetProperty(ref _selectedTabHeader, value); }
}
private List<StepModel> _SelectedItems;
public List<StepModel> SelectedItems
private List<StepVM> _SelectedItems;
public List<StepVM> SelectedItems
{
get { return _SelectedItems; }
set { SetProperty(ref _SelectedItems, value); }
}
private StepModel _SelectedStep;
public StepModel SelectedStep
private StepVM _SelectedStep;
public StepVM SelectedStep
{
get => _SelectedStep;
set
@@ -62,7 +62,7 @@ namespace TestingModule.ViewModels
}
}
}
public ProgramModel Program
public ProgramVM Program
{
get => _ScopedContext.Program;
set
@@ -77,7 +77,7 @@ namespace TestingModule.ViewModels
ScopedContext _ScopedContext { get; set; }
private readonly SystemConfig _systemConfig;
private readonly GlobalInfo _globalInfo;
private List<StepModel> tmpCopyList = new List<StepModel>();
private List<StepVM> tmpCopyList = new List<StepVM>();
#endregion
@@ -115,7 +115,7 @@ namespace TestingModule.ViewModels
var selectedList = parameter as IList;
if (selectedList != null)
{
SelectedItems = selectedList.Cast<StepModel>().ToList();
SelectedItems = selectedList.Cast<StepVM>().ToList();
}
}
@@ -157,7 +157,7 @@ namespace TestingModule.ViewModels
foreach (var item in tmpCopyList)
{
// 创建新副本,避免引用同一个对象,并赋予新 ID
var newStep = new StepModel(item) { ID = Guid.NewGuid() };
var newStep = new StepVM(item) { ID = Guid.NewGuid() };
Program.StepCollection.Insert(insertIndex, newStep);
insertIndex++; // 递增索引,保证粘贴的多项顺序一致
}
@@ -168,7 +168,7 @@ namespace TestingModule.ViewModels
foreach (var item in tmpCopyList)
{
var newStep = new StepModel(item) { ID = Guid.NewGuid() };
var newStep = new StepVM(item) { ID = Guid.NewGuid() };
Program.ErrorStepCollection.Insert(insertIndex, newStep);
insertIndex++;
}
@@ -208,7 +208,7 @@ namespace TestingModule.ViewModels
#region
private void StepCollection_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
var collection = sender as ObservableCollection<StepModel>;
var collection = sender as ObservableCollection<StepVM>;
// Add/Move/Remove 都会触发,这里判断具体情形
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add ||
e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Move||
@@ -231,7 +231,50 @@ namespace TestingModule.ViewModels
}
}
#endregion
public void Dispose()
{
try
{
// 1. 【核心修复】必须取消订阅 CollectionChanged 事件,否则 VM 永远无法被释放
if (Program != null)
{
if (Program.StepCollection != null)
{
Program.StepCollection.CollectionChanged -= StepCollection_CollectionChanged;
}
if (Program.ErrorStepCollection != null)
{
Program.ErrorStepCollection.CollectionChanged -= StepCollection_CollectionChanged;
}
}
// 2. 【核心修复】必须显式退订 Prism 全局事件AlarmEvent
// 注意:因为订阅时使用的是匿名 Lambda最安全稳妥的退订方式是把整个事件上的当前 VM 订阅者全部注销
_eventAggregator?.GetEvent<AlarmEvent>()?.Unsubscribe(null);
// 3. 清空临时缓存集合与 UI 绑定列表,避免悬挂指针
tmpCopyList?.Clear();
tmpCopyList = null!;
SelectedItems?.Clear();
SelectedItems = null!;
// 4. 清除选中项状态引用
SelectedStep = null;
if (_ScopedContext != null)
{
_ScopedContext.SelectedStep = null;
_ScopedContext = null!; // 断开上下文引用
}
}
catch (Exception ex)
{
Logger.LoggerHelper.Error($"释放步骤管理组件StepsManagerViewModel资源失败: {ex.Message}");
}
}
}
}

View File

@@ -51,7 +51,7 @@
<TreeView Grid.Row="1"
ItemsSource="{Binding InstructionTree}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type model:InstructionNode}"
<HierarchicalDataTemplate DataType="{x:Type model:InstructionNodeVM}"
ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>

View File

@@ -3,6 +3,7 @@
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:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:prism="http://prismlibrary.com/"
xmlns:converters="clr-namespace:UIShare.Converters;assembly=UIShare"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
@@ -18,7 +19,11 @@
<converters:ParameterCategoryToStringConverter x:Key="ParameterCategoryToStringConverter" />
<converters:ParameterValueToStringConverter x:Key="ParameterValueToStringConverter" />
</UserControl.Resources>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding LoadedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<GroupBox Header="设备/参数">
<TabControl>
@@ -69,7 +74,7 @@
<TabItem Header="设备">
<DataGrid Padding="10"
Background="Transparent"
ItemsSource="{Binding DeviceInfoModel}"
ItemsSource="{Binding DeviceInfoVM}"
AutoGenerateColumns="False"
CanUserAddRows="False"
IsReadOnly="True"
@@ -123,7 +128,7 @@
<MenuItem Header="编辑"
Command="{Binding DeviceEditCommand}" />
<MenuItem Header="重新连接"
Command="{Binding ReconnnectCommand}" />
Command="{Binding ReConnectCommand}" />
<MenuItem Header="关闭"
Command="{Binding CloseCommand}" />
</ContextMenu>

View File

@@ -21,7 +21,7 @@ namespace UIShare.Converters
return allParameters;
// 过滤出类型匹配的参数
return allParameters.Cast<ParameterModel>()
return allParameters.Cast<ParameterVM>()
.Where(p => IsTypeMatch(currentParamType, p.Type))
.ToList();
}

View File

@@ -5,7 +5,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using static UIShare.UIViewModel.ParameterModel;
using static UIShare.UIViewModel.ParameterVM;

View File

@@ -6,7 +6,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using static UIShare.UIViewModel.ParameterModel;
using static UIShare.UIViewModel.ParameterVM;

View File

@@ -18,9 +18,7 @@ namespace UIShare.GlobalVariable
return false;
}
// 临时实例化一个对象以获取默认的 SystemPath
var dummy = new SystemConfig();
string configPath = Path.Combine(dummy.SystemPath, $"{title}.json");
string configPath = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ADP"), $"{title}.json");
if (!File.Exists(configPath))
{

View File

@@ -0,0 +1,303 @@
using DeviceCommand.Base;
using Logger;
using Model.Models;
using Prism.Ioc;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using UIShare.UIViewModel;
namespace UIShare.GlobalVariable
{
/// <summary>
/// 设备管理器:根据 <see cref="SystemConfig.DeviceList"/> 反射实例化所有启用的设备,
/// 通过 <see cref="IBaseInterface"/> 多态统一管理,避免为每种设备单独硬编码字段。
/// </summary>
public class DeviceManager
{
private object _lockObj = new object();
public SystemConfig _systemConfig { get; set; }
/// <summary>按 DeviceName 索引的设备字典,便于业务层按名取实例。</summary>
public IDictionary<string, IBaseInterface> DeviceMap { get; private set; }
= new Dictionary<string, IBaseInterface>(StringComparer.OrdinalIgnoreCase);
/// <summary>类名 → Type 的反射缓存(仅扫描一次)。</summary>
private static readonly IReadOnlyDictionary<string, Type> _deviceTypeMap = BuildDeviceTypeMap();
public DeviceManager(SystemConfig systemConfig)
{
_systemConfig = systemConfig;
InitDevices();
}
private void InitDevices()
{
DeviceMap = new Dictionary<string, IBaseInterface>(StringComparer.OrdinalIgnoreCase);
if (_systemConfig?.DeviceList == null) return;
foreach (var config in _systemConfig.DeviceList)
{
if (config == null || !config.IsEnabled) continue;
if (string.IsNullOrWhiteSpace(config.DeviceType) ||
!_deviceTypeMap.TryGetValue(config.DeviceType, out var deviceType))
{
LoggerHelper.Warn($"未识别的设备类型 [{config.DeviceType}],已跳过 [{config.DeviceName}]。");
continue;
}
try
{
IBaseInterface? instance = config.ConnectionType switch
{
"Tcp" => CreateTcpDevice(deviceType, config.TcpConfig),
"Serial" => CreateSerialDevice(deviceType, config.SerialPortConfig),
_ => null
};
if (instance == null)
{
LoggerHelper.Warn($"设备 [{config.DeviceName}] 连接方式 [{config.ConnectionType}] 不支持,已跳过。");
continue;
}
if (!string.IsNullOrWhiteSpace(config.DeviceName))
{
DeviceMap[config.DeviceName] = instance;
}
LoggerHelper.Info($"已加载设备 [{config.DeviceName} / {config.DeviceType} / {config.ConnectionType}]");
}
catch (Exception ex)
{
var inner = ex.InnerException?.Message ?? ex.Message;
LoggerHelper.ErrorWithNotify($"设备 [{config.DeviceName}] 实例化失败:{inner}");
}
}
}
public async Task ConnectAllDevices(CancellationToken ct = default)
{
if (_systemConfig?.DeviceList == null || DeviceMap.Count == 0) return;
var tasks = new List<Task>();
foreach (var info in _systemConfig.DeviceList)
{
if (info == null || !info.IsEnabled) continue;
if (string.IsNullOrWhiteSpace(info.DeviceName)) continue;
if (!DeviceMap.TryGetValue(info.DeviceName, out var device)) continue;
tasks.Add(ConnectInternalAsync(info, device, ct));
}
await Task.WhenAll(tasks);
}
public async Task ConnectSpecifiedDevice(string deviceName, CancellationToken ct = default)
{
if (string.IsNullOrWhiteSpace(deviceName))
{
LoggerHelper.Warn("ConnectSpecifiedDevice设备名为空。");
return;
}
if (!DeviceMap.TryGetValue(deviceName, out var device))
{
LoggerHelper.Warn($"ConnectSpecifiedDevice未找到设备 [{deviceName}]。");
return;
}
var info = _systemConfig?.DeviceList?
.FirstOrDefault(d => d != null && string.Equals(d.DeviceName, deviceName, StringComparison.OrdinalIgnoreCase));
await ConnectInternalAsync(info, device, ct);
}
/// <summary>
/// 异步关闭指定设备,释放底层连接并更新 UI 状态
/// </summary>
public async Task CloseDeviceAsync(string deviceName)
{
if (string.IsNullOrWhiteSpace(deviceName)) return;
IBaseInterface? device;
DeviceInfoVM? info;
lock (_lockObj)
{
if (!DeviceMap.TryGetValue(deviceName, out device)) return;
info = _systemConfig?.DeviceList?
.FirstOrDefault(d => d != null && string.Equals(d.DeviceName, deviceName, StringComparison.OrdinalIgnoreCase));
}
await CloseInternalAsync(info, device);
}
/// <summary>
/// 异步关闭所有设备
/// </summary>
public async Task CloseAllDevicesAsync()
{
List<Task> tasks = new List<Task>();
lock (_lockObj)
{
if (DeviceMap.Count == 0) return;
foreach (var kvp in DeviceMap)
{
string deviceName = kvp.Key;
var device = kvp.Value;
var info = _systemConfig?.DeviceList?
.FirstOrDefault(d => d != null && string.Equals(d.DeviceName, deviceName, StringComparison.OrdinalIgnoreCase));
tasks.Add(CloseInternalAsync(info, device));
}
}
await Task.WhenAll(tasks);
LoggerHelper.Info("所有设备已执行关闭操作。");
}
#region
private async Task CloseInternalAsync(DeviceInfoVM? info, IBaseInterface device)
{
string name = info?.DeviceName ?? device.GetType().Name;
string conn = info?.ConnectionType ?? "?";
try
{
// 如果设备本身已经是断开状态,直接更新 UI 并返回
if (!device.IsConnected)
{
if (info != null) info.IsConnected = false;
LoggerHelper.Info($"设备 [{name}] 本就处于断开状态。");
return;
}
await Task.Run(() => device.Close());
LoggerHelper.Info($"设备 [{name}/{conn}] 已成功关闭连接。");
}
catch (Exception ex)
{
var inner = ex.InnerException?.Message ?? ex.Message;
LoggerHelper.Error($"设备 [{name}/{conn}] 关闭连接时出现异常: {inner}");
}
finally
{
// 无论关闭时是否抛出异常,均强制同步 UI 状态为未连接
if (info != null)
{
info.IsConnected = false;
}
}
}
private async Task ConnectInternalAsync(DeviceInfoVM? info, IBaseInterface device, CancellationToken ct)
{
string name = info?.DeviceName ?? device.GetType().Name;
string conn = info?.ConnectionType ?? "?";
try
{
if (device.IsConnected)
{
if (info != null) info.IsConnected = true;
LoggerHelper.Info($"设备 [{name}] 已连接,跳过。");
return;
}
bool ok = conn switch
{
"Tcp" => await ConnectTcpAsync(name, device, ct),
"Serial" => await ConnectSerialAsync(name, device, ct),
_ => false
};
if (info != null) info.IsConnected = ok;
if (ok)
LoggerHelper.Info($"设备 [{name}/{conn}] 连接成功。");
else
LoggerHelper.Warn($"设备 [{name}/{conn}] 连接失败。");
}
catch (OperationCanceledException)
{
if (info != null) info.IsConnected = false;
LoggerHelper.Warn($"设备 [{name}/{conn}] 连接已取消。");
}
catch (Exception ex)
{
if (info != null) info.IsConnected = false;
var inner = ex.InnerException?.Message ?? ex.Message;
LoggerHelper.ErrorWithNotify($"设备 [{name}/{conn}] 连接异常:{inner}");
}
}
private static async Task<bool> ConnectTcpAsync(string name, IBaseInterface device, CancellationToken ct)
{
if (device is not ITcp tcp)
{
LoggerHelper.Warn($"设备 [{name}] 配置为 Tcp 但未实现 ITcp实际类型为 {device.GetType().Name}。");
return false;
}
return await tcp.ConnectAsync(ct);
}
private static async Task<bool> ConnectSerialAsync(string name, IBaseInterface device, CancellationToken ct)
{
if (device is not ISerialPort sp)
{
LoggerHelper.Warn($"设备 [{name}] 配置为 Serial 但未实现 ISerialPort实际类型为 {device.GetType().Name}。");
return false;
}
return await sp.ConnectAsync(ct);
}
private static IReadOnlyDictionary<string, Type> BuildDeviceTypeMap()
{
try
{
return typeof(IBaseInterface).Assembly
.GetTypes()
.Where(t => t.IsClass && !t.IsAbstract && typeof(IBaseInterface).IsAssignableFrom(t))
.ToDictionary(t => t.Name, t => t, StringComparer.OrdinalIgnoreCase);
}
catch (ReflectionTypeLoadException ex)
{
LoggerHelper.Error($"扫描设备类型失败:{ex.Message}");
return new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
}
}
private static IBaseInterface? CreateTcpDevice(Type type, TcpConfigVM? vm)
{
vm ??= new TcpConfigVM();
var cfg = new TcpConfig
{
IPAddress = vm.IPAddress,
Port = vm.Port,
SendTimeout = vm.SendTimeout,
ReceiveTimeout = vm.ReceiveTimeout
};
return Activator.CreateInstance(type, cfg) as IBaseInterface;
}
private static IBaseInterface? CreateSerialDevice(Type type, SerialPortConfigVM? vm)
{
vm ??= new SerialPortConfigVM();
var cfg = new SerialPortConfig
{
PortName = vm.PortName,
BaudRate = vm.BaudRate,
DataBits = vm.DataBits,
StopBits = Enum.TryParse<StopBits>(vm.StopBits, true, out var sb) ? sb : StopBits.One,
Parity = Enum.TryParse<Parity>(vm.Parity, true, out var pa) ? pa : Parity.None,
ReadTimeout = vm.ReadTimeout,
WriteTimeout = vm.WriteTimeout
};
return Activator.CreateInstance(type, cfg) as IBaseInterface;
}
#endregion
}
}

View File

@@ -11,9 +11,11 @@ namespace UIShare.GlobalVariable
public event EventHandler? ScopeChanged;
public Dictionary<string,ScopedContext> ContextDic { get; set; }
public Dictionary<string,StepRunning> StepRunningDic { get; set; }
public Dictionary<string, SystemConfig> ConfigDic { get; set; }
public Dictionary<string, IScopedProvider> ScopeDic { get; set; }
public String UserName { get; set; } = "Not Logged in";
public bool IsAdmin { get; set; } = true;
public string CurrentOpeningScope;
private string _currentScope = "default";
public string CurrentScope
{
@@ -31,6 +33,7 @@ namespace UIShare.GlobalVariable
{
ContextDic = new();
StepRunningDic = new();
ConfigDic = new();
ScopeDic = new();
CurrentScope = "default";
}

View File

@@ -15,7 +15,7 @@ namespace UIShare.GlobalVariable
public class ScopedContext
{
private static readonly Random _randomSeed = new Random();
public ProgramModel Program { get; set; } = new();
public ProgramVM Program { get; set; } = new();
public String SelectedStepList { get; set; } = "主程序";
public string CurrentFilePath { get; set; }
public bool? IsStop { get; set; }
@@ -26,8 +26,8 @@ namespace UIShare.GlobalVariable
public bool IsTerminate { get; set; } = false;
public ObservableCollection<Assembly> Assemblies { get; set; } = new();
public PackIconKind RunIcon { get; set; } = PackIconKind.Play;
public StepModel SelectedStep { get; set; }
public ParameterModel SelectedParameter { get; set; }
public StepVM SelectedStep { get; set; }
public ParameterVM SelectedParameter { get; set; }
public List<IBaseInterface> DeviceList { get; set; } = new();
// 【新增测试属性】:每个实例被 new 出来时独一无二的随机身份

View File

@@ -13,7 +13,7 @@ using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UIShare.GlobalVariable;
using static UIShare.UIViewModel.ParameterModel;
using static UIShare.UIViewModel.ParameterVM;
namespace UIShare
@@ -22,11 +22,12 @@ namespace UIShare
{
private ScopedContext _scopedContext;
private SystemConfig _systemConfig;
private DeviceManager _deviceManager;
//private Devices _devices;
private IContainerProvider containerProvider;
private IEventAggregator _eventAggregator;
private readonly Dictionary<Guid, ParameterModel> tmpParameters = [];
private readonly Dictionary<Guid, ParameterVM> tmpParameters = [];
private readonly Stopwatch stepStopwatch = new();
@@ -39,14 +40,15 @@ namespace UIShare
private bool SubSingleStep = false;
private Guid TestRoundID;
public StepRunning(ScopedContext ScopedContext, SystemConfig systemConfig,IEventAggregator eventAggregator,IContainerProvider containerProvider)
public StepRunning(ScopedContext ScopedContext, SystemConfig systemConfig,IEventAggregator eventAggregator, DeviceManager deviceManager)
{
_scopedContext = ScopedContext;
_systemConfig = systemConfig;
_eventAggregator = eventAggregator;
_deviceManager= deviceManager;
//_devices = containerProvider.Resolve<Devices>();
}
public async Task<bool> ExecuteErrorSteps(ProgramModel program, int depth = 0, CancellationToken cancellationToken = default)
public async Task<bool> ExecuteErrorSteps(ProgramVM program, int depth = 0, CancellationToken cancellationToken = default)
{
int index = 0;
bool stepSuccess = false;
@@ -211,7 +213,7 @@ namespace UIShare
return loopStack.Count == 0 && stepSuccess;
}
public async Task<bool> ExecuteSteps(ProgramModel program, int depth = 0, CancellationToken cancellationToken = default)
public async Task<bool> ExecuteSteps(ProgramVM program, int depth = 0, CancellationToken cancellationToken = default)
{
int index = 0;
bool stepSuccess = false;
@@ -388,7 +390,7 @@ namespace UIShare
return loopStack.Count == 0 && stepSuccess;
}
public async Task ExecuteMethodStep(StepModel step, Dictionary<Guid, ParameterModel> parameters, int depth, CancellationToken cancellationToken = default)
public async Task ExecuteMethodStep(StepVM step, Dictionary<Guid, ParameterVM> parameters, int depth, CancellationToken cancellationToken = default)
{
try
{
@@ -417,7 +419,7 @@ namespace UIShare
// 3. 准备参数
var inputParams = new List<object?>();
var paramTypes = new List<Type>();
ParameterModel? outputParam = null;
ParameterVM? outputParam = null;
foreach (var param in step.Method!.Parameters)
{
if (param.Category == ParameterCategory.Input)
@@ -656,7 +658,7 @@ namespace UIShare
}
}
public void ResetAllStepStatus(ObservableCollection<StepModel> StepCollection)
public void ResetAllStepStatus(ObservableCollection<StepVM> StepCollection)
{
foreach (var step in StepCollection)
{
@@ -665,7 +667,7 @@ namespace UIShare
}
}
private void UpdateCurrentStepResult(StepModel step, bool paraResult = true, bool stepResult = true, int depth = 0)
private void UpdateCurrentStepResult(StepVM step, bool paraResult = true, bool stepResult = true, int depth = 0)
{
if (stepResult && paraResult)
{
@@ -721,7 +723,7 @@ namespace UIShare
public int LoopCount { get; set; }
public int CurrentLoop { get; set; }
public int StartIndex { get; set; }
public StepModel? LoopStartStep { get; set; }
public StepVM? LoopStartStep { get; set; }
}
#endregion

View File

@@ -26,10 +26,10 @@ namespace UIShare.GlobalVariable
public string DefaultProgramFilePath { get; set; } = "";
public string DefaultBLFFilePath { get; set; } = "";
public string DefaultDBCFilePath { get; set; } = "";
public ObservableCollection<DeviceInfoModel> DeviceList = new();
// public ObservableCollection<DeviceInfoModel> DeviceList { get; set; } = new()
public ObservableCollection<DeviceInfoVM> DeviceList = new();
// public ObservableCollection<DeviceInfoVM> DeviceList { get; set; } = new()
//{
// new DeviceInfoModel
// new DeviceInfoVM
// {
// DeviceName = "IT7800E",
// DeviceType = "IT7800E",
@@ -39,7 +39,7 @@ namespace UIShare.GlobalVariable
// IsConnected = false
// },
// new DeviceInfoModel
// new DeviceInfoVM
// {
// DeviceName = "N36200",
// DeviceType = "N36200",
@@ -49,7 +49,7 @@ namespace UIShare.GlobalVariable
// IsConnected = false
// },
// new DeviceInfoModel
// new DeviceInfoVM
// {
// DeviceName = "N36600",
// DeviceType = "N36600",
@@ -59,7 +59,7 @@ namespace UIShare.GlobalVariable
// IsConnected = false
// },
// new DeviceInfoModel
// new DeviceInfoVM
// {
// DeviceName = "N69200",
// DeviceType = "N69200",
@@ -69,7 +69,7 @@ namespace UIShare.GlobalVariable
// IsConnected = false
// },
// new DeviceInfoModel
// new DeviceInfoVM
// {
// DeviceName = "SDS2000X_HD",
// DeviceType = "SDS2000X_HD",
@@ -79,7 +79,7 @@ namespace UIShare.GlobalVariable
// IsConnected = false
// },
// new DeviceInfoModel
// new DeviceInfoVM
// {
// DeviceName = "SPAW7000",
// DeviceType = "SPAW7000",

View File

@@ -0,0 +1,21 @@
using UIShare.UIViewModel;
namespace UIShare.PubEvent
{
/// <summary>
/// 其他模块向弹窗管理器添加 Tab 的事件。
/// 发布方:任何需要将弹窗托管到 DialogManagerView 的模块。
/// 订阅方:<c>DialogMangerViewModel</c>(仅一处订阅)。
/// <code>
/// // 示例:在某个 ViewModel 中发布
/// _eventAggregator.GetEvent&lt;AddDialogTabEvent&gt;().Publish(new DialogTabInfo
/// {
/// Title = "设备配置",
/// Content = new DeviceConfigView()
/// });
/// </code>
/// </summary>
public class AddDialogTabEvent : PubSubEvent<DialogTabInfo>
{
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UIShare.PubEvent
{
public class CancelMinimizeEvent:PubSubEvent
{
}
}

View File

@@ -1,6 +1,6 @@
namespace UIShare.UIViewModel
{
public class CanMessageShowModel : BindableBase
public class CanMessageShowVM : BindableBase
{
// 字段声明
private byte _通道;

View File

@@ -2,17 +2,17 @@ using Prism.Mvvm;
namespace UIShare.UIViewModel
{
public class CustomPanelItem : BindableBase
public class CustomPanelItemVM : BindableBase
{
private string _name;
private string _pointY;
public string Name
{
get => _name;
set => SetProperty(ref _name, value);
}
private string _pointY;
public string PointY
{
get => _pointY;

View File

@@ -2,7 +2,7 @@
namespace UIShare.UIViewModel
{
public class DeviceInfoModel : BindableBase
public class DeviceInfoVM : BindableBase
{
private string _deviceName;
public string DeviceName
@@ -43,7 +43,7 @@ namespace UIShare.UIViewModel
/// 连接方式:"None" / "TCP" / "Serial"。
/// 用于决定 SettingView 上"配置..."按钮打开哪一个对话框。
/// </summary>
private string _connectionType = "None";
private string _connectionType ;
public string ConnectionType
{
get => _connectionType;
@@ -51,16 +51,16 @@ namespace UIShare.UIViewModel
}
/// <summary>TCP 连接参数(首次访问时自动初始化,便于 XAML 直接绑定)。</summary>
private TcpConnectionConfig _tcpConfig = new();
public TcpConnectionConfig TcpConfig
private TcpConfigVM _tcpConfig = new();
public TcpConfigVM TcpConfig
{
get => _tcpConfig;
set => SetProperty(ref _tcpConfig, value);
}
/// <summary>串口连接参数(首次访问时自动初始化,便于 XAML 直接绑定)。</summary>
private SerialPortConnectionConfig _serialPortConfig = new();
public SerialPortConnectionConfig SerialPortConfig
private SerialPortConfigVM _serialPortConfig = new();
public SerialPortConfigVM SerialPortConfig
{
get => _serialPortConfig;
set => SetProperty(ref _serialPortConfig, value);

View File

@@ -0,0 +1,19 @@
namespace UIShare.UIViewModel
{
/// <summary>
/// 弹窗 Tab 信息载体(事件负载,轻量 POCO
/// 其他模块发布 <c>AddDialogTabEvent</c> 时填充此对象,
/// DialogMangerViewModel 接收后创建对应的 Tab 项。
/// </summary>
public class DialogTabInfo
{
/// <summary>Tab 标题(显示在标签页上)。</summary>
public string Title { get; set; } = string.Empty;
/// <summary>
/// Tab 内容:传入一个已实例化的 <see cref="System.Windows.FrameworkElement"/>(通常是 UserControl
/// 由 ContentControl 直接承载展示。
/// </summary>
public object? Content { get; set; }
}
}

View File

@@ -1,18 +0,0 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace UIShare.UIViewModel
{
public class InstructionNode
{
public string Name { get; set; }
public ObservableCollection<InstructionNode> Children { get; set; } = new();
public object Tag { get; set; }
}
}

View File

@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Prism.Mvvm; // 确保引入了 Prism 命名空间
namespace UIShare.UIViewModel
{
public class InstructionNodeVM : BindableBase
{
private string _name = string.Empty;
public string Name
{
get => _name;
set => SetProperty(ref _name, value);
}
private ObservableCollection<InstructionNodeVM> _children = new();
public ObservableCollection<InstructionNodeVM> Children
{
get => _children;
set => SetProperty(ref _children, value);
}
private object? _tag;
public object? Tag
{
get => _tag;
set => SetProperty(ref _tag, value);
}
}
}

View File

@@ -7,17 +7,17 @@ using System.Threading.Tasks;
namespace UIShare.UIViewModel
{
public class MethodModel
public class MethodVM
{
#region
public MethodModel()
public MethodVM()
{
}
public MethodModel(MethodModel source)
public MethodVM(MethodVM source)
{
if (source == null) return;
@@ -25,8 +25,8 @@ namespace UIShare.UIViewModel
FullName = source.FullName;
// 深拷贝参数
Parameters = new ObservableCollection<ParameterModel>(
source.Parameters.Select(p => new ParameterModel(p)));
Parameters = new ObservableCollection<ParameterVM>(
source.Parameters.Select(p => new ParameterVM(p)));
}
#endregion
@@ -35,6 +35,6 @@ namespace UIShare.UIViewModel
public string? FullName { get; set; }
public ObservableCollection<ParameterModel> Parameters { get; set; } = [];
public ObservableCollection<ParameterVM> Parameters { get; set; } = [];
}
}

View File

@@ -5,16 +5,16 @@ using System.Collections.Generic;
namespace UIShare.UIViewModel
{
public class ParameterModel : BindableBase
public class ParameterVM : BindableBase
{
#region
public ParameterModel()
public ParameterVM()
{
}
public ParameterModel(ParameterModel source)
public ParameterVM(ParameterVM source)
{
if (source == null) return;
@@ -139,10 +139,10 @@ namespace UIShare.UIViewModel
Temp
}
public object? GetActualValue(Dictionary<Guid, ParameterModel> paraList)
public object? GetActualValue(Dictionary<Guid, ParameterVM> paraList)
{
HashSet<Guid> visitedIds = new HashSet<Guid>();
ParameterModel current = this;
ParameterVM current = this;
while (current != null)
{
@@ -172,7 +172,7 @@ namespace UIShare.UIViewModel
}
}
ParameterModel? next = paraList[(Guid)current.VariableID!];
ParameterVM? next = paraList[(Guid)current.VariableID!];
if (next == null)
{
return null;
@@ -184,10 +184,10 @@ namespace UIShare.UIViewModel
return null;
}
public ParameterModel? GetCurrentParameter(Dictionary<Guid, ParameterModel> paraList)
public ParameterVM? GetCurrentParameter(Dictionary<Guid, ParameterVM> paraList)
{
HashSet<Guid> visitedIds = new HashSet<Guid>();
ParameterModel current = this;
ParameterVM current = this;
while (current != null)
{
@@ -217,7 +217,7 @@ namespace UIShare.UIViewModel
}
}
ParameterModel? next = paraList[(Guid)current.VariableID!];
ParameterVM? next = paraList[(Guid)current.VariableID!];
if (next == null)
{
return null;

View File

@@ -1,51 +0,0 @@
using Prism.Mvvm; // 引入 Prism 的 BindableBase
using System;
using System.Collections.ObjectModel;
using System.Linq;
namespace UIShare.UIViewModel
{
public class ProgramModel : BindableBase
{
#region
public ProgramModel()
{
// 可以进行初始化操作
}
public ProgramModel(ProgramModel source)
{
ID = source.ID;
StepCollection = new ObservableCollection<StepModel>(source.StepCollection.Select(p => new StepModel(p)));
ErrorStepCollection = new ObservableCollection<StepModel>(source.ErrorStepCollection.Select(p => new StepModel(p)));
Parameters = new ObservableCollection<ParameterModel>(source.Parameters.Select(p => new ParameterModel(p)));
}
#endregion
public Guid ID { get; set; } = Guid.NewGuid();
private ObservableCollection<StepModel> _stepCollection = new ObservableCollection<StepModel>();
public ObservableCollection<StepModel> StepCollection
{
get => _stepCollection;
set => SetProperty(ref _stepCollection, value);
}
private ObservableCollection<StepModel> _errorStepCollection = new ObservableCollection<StepModel>();
public ObservableCollection<StepModel> ErrorStepCollection
{
get => _errorStepCollection;
set => SetProperty(ref _errorStepCollection, value);
}
private ObservableCollection<ParameterModel> _parameters = new ObservableCollection<ParameterModel>();
public ObservableCollection<ParameterModel> Parameters
{
get => _parameters;
set => SetProperty(ref _parameters, value);
}
}
}

View File

@@ -0,0 +1,51 @@
using Prism.Mvvm; // 引入 Prism 的 BindableBase
using System;
using System.Collections.ObjectModel;
using System.Linq;
namespace UIShare.UIViewModel
{
public class ProgramVM : BindableBase
{
#region
public ProgramVM()
{
// 可以进行初始化操作
}
public ProgramVM(ProgramVM source)
{
ID = source.ID;
StepCollection = new ObservableCollection<StepVM>(source.StepCollection.Select(p => new StepVM(p)));
ErrorStepCollection = new ObservableCollection<StepVM>(source.ErrorStepCollection.Select(p => new StepVM(p)));
Parameters = new ObservableCollection<ParameterVM>(source.Parameters.Select(p => new ParameterVM(p)));
}
#endregion
public Guid ID { get; set; } = Guid.NewGuid();
private ObservableCollection<StepVM> _stepCollection = new ObservableCollection<StepVM>();
public ObservableCollection<StepVM> StepCollection
{
get => _stepCollection;
set => SetProperty(ref _stepCollection, value);
}
private ObservableCollection<StepVM> _errorStepCollection = new ObservableCollection<StepVM>();
public ObservableCollection<StepVM> ErrorStepCollection
{
get => _errorStepCollection;
set => SetProperty(ref _errorStepCollection, value);
}
private ObservableCollection<ParameterVM> _parameters = new ObservableCollection<ParameterVM>();
public ObservableCollection<ParameterVM> Parameters
{
get => _parameters;
set => SetProperty(ref _parameters, value);
}
}
}

View File

@@ -1,68 +1,16 @@
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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
public class SerialPortConfigVM : BindableBase
{
private string _portName = "COM1";
public string PortName
@@ -115,9 +63,9 @@ namespace UIShare.UIViewModel
set => SetProperty(ref _writeTimeout, value);
}
public SerialPortConnectionConfig() { }
public SerialPortConfigVM() { }
public SerialPortConnectionConfig(SerialPortConnectionConfig? src)
public SerialPortConfigVM(SerialPortConfigVM? src)
{
if (src == null) return;
PortName = src.PortName;
@@ -129,7 +77,7 @@ namespace UIShare.UIViewModel
WriteTimeout = src.WriteTimeout;
}
public void CopyTo(SerialPortConnectionConfig? dst)
public void CopyTo(SerialPortConfigVM? dst)
{
if (dst == null) return;
dst.PortName = PortName;

View File

@@ -4,13 +4,13 @@ using System;
namespace UIShare.UIViewModel
{
public class StepModel : BindableBase
public class StepVM : BindableBase
{
#region
public StepModel() { }
public StepVM() { }
public StepModel(StepModel source)
public StepVM(StepVM source)
{
if (source == null) return;
@@ -29,11 +29,11 @@ namespace UIShare.UIViewModel
if (source.Method != null)
{
Method = new MethodModel(source.Method);
Method = new MethodVM(source.Method);
}
if (source.SubProgram != null)
{
SubProgram = new ProgramModel(source.SubProgram);
SubProgram = new ProgramVM(source.SubProgram);
}
}
@@ -79,17 +79,17 @@ namespace UIShare.UIViewModel
set => SetProperty(ref _stepType, value);
}
private MethodModel? _method;
private MethodVM? _method;
public MethodModel? Method
public MethodVM? Method
{
get => _method;
set => SetProperty(ref _method, value);
}
private ProgramModel? _subProgram;
private ProgramVM? _subProgram;
public ProgramModel? SubProgram
public ProgramVM? SubProgram
{
get => _subProgram;
set => SetProperty(ref _subProgram, value);

View File

@@ -1,15 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UIShare.UIViewModel
{
public class SubProgramItem
{
public string Name { get; set; } = "";
public string FilePath { get; set; } = "";
}
}

View File

@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UIShare.UIViewModel
{
public class SubProgramItemVM : BindableBase
{
private string _name="";
public string Name
{
get => _name;
set => SetProperty(ref _name, value);
}
private string _filePath = "";
public string FilePath
{
get => _filePath;
set => SetProperty(ref _filePath, value);
}
}
}

View File

@@ -0,0 +1,62 @@
using Prism.Mvvm;
namespace UIShare.UIViewModel
{
/// <summary>
/// TCP 连接配置(与 DeviceCommand.Base.Tcp 保持字段一致)。
/// </summary>
public class TcpConfigVM : 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 TcpConfigVM() { }
/// <summary>拷贝构造,用于对话框编辑副本。</summary>
public TcpConfigVM(TcpConfigVM? src)
{
if (src == null) return;
IPAddress = src.IPAddress;
Port = src.Port;
SendTimeout = src.SendTimeout;
ReceiveTimeout = src.ReceiveTimeout;
}
/// <summary>把字段拷回目标对象(保存时用)。</summary>
public void CopyTo(TcpConfigVM? dst)
{
if (dst == null) return;
dst.IPAddress = IPAddress;
dst.Port = Port;
dst.SendTimeout = SendTimeout;
dst.ReceiveTimeout = ReceiveTimeout;
}
}
}