共有变量添加

This commit is contained in:
hsc
2026-06-23 14:21:13 +08:00
parent 5b721d2557
commit cd3cbe7c35
11 changed files with 174 additions and 34 deletions

View File

@@ -1,6 +1,7 @@
using Logger;
using MaterialDesignThemes.Wpf;
using Model.Models;
using Notifications.Wpf.Core;
using System.Collections.Concurrent;
using System.Diagnostics;
@@ -495,7 +496,15 @@ namespace ADP.ViewModels
targetContext.Program.Parameters.Clear();
targetContext.Program.StepCollection.Clear();
targetContext.Program.ErrorStepCollection.Clear();
foreach (var item in CurrentConfig.ParameterList)
{
var param = CurrentConfig.SharedParameterList.FirstOrDefault(x => x.ParameterName == item.Name);
if (param != null)
{
item.Value = param.Value;
}
targetContext.Program.Parameters.Add(item);
}
LoggerHelper.InfoWithNotify($"工位 [{runningScope}] 创建了空程序文件");
// 如果当前正看着该工位,刷新 UI 显示
@@ -552,6 +561,16 @@ namespace ADP.ViewModels
targetContext.Program.ErrorStepCollection = program.ErrorStepCollection;
targetContext.CurrentFilePath = filePath;
foreach (var item in CurrentConfig.SharedParameterList)
{
var parameter = targetContext?.Program?.Parameters?.FirstOrDefault(x => x.Name == item.ParameterName);
if (parameter != null)
{
parameter.Value = item.Value;
}
}
LoggerHelper.SuccessWithNotify($"工位 [{runningScope}] 成功打开文件: {filePath}");
// 💡 3. 安全调用异步复位,确保重置的是对应工位的数据

View File

@@ -173,6 +173,15 @@ namespace MainModule.ViewModels
_scopedContext.Program.StepCollection = program.StepCollection;
_scopedContext.Program.ErrorStepCollection = program.ErrorStepCollection;
_scopedContext.CurrentFilePath = filePath;
foreach (var item in _systemConfig.SharedParameterList)
{
var parameter = _scopedContext?.Program?.Parameters?.FirstOrDefault(x => x.Name == item.ParameterName);
if (parameter != null)
{
parameter.Value = item.Value;
}
}
}
}

View File

@@ -24,7 +24,6 @@ namespace Model.Models
/// </summary>
public string? Category { get; set; }
public bool IsGlobal { get; set; }
public object? Value { get; set; }
@@ -36,7 +35,6 @@ namespace Model.Models
public bool IsUseVar { get; set; }
public bool IsSave { get; set; }
public string? VariableName { get; set; }

View File

@@ -44,6 +44,11 @@ namespace SettingModule.ViewModels
{
get => _deviceList;
set => SetProperty(ref _deviceList, value);
}
public ObservableCollection<SharedParameter> SharedParameterList
{
get => _sharedParameterList;
set => SetProperty(ref _sharedParameterList, value);
}
public string StatusMessage
@@ -72,6 +77,7 @@ namespace SettingModule.ViewModels
private string _testStatus = string.Empty;
private DeviceInfoVM? _selectedDevice;
private ObservableCollection<DeviceInfoVM> _deviceList;
private ObservableCollection<SharedParameter> _sharedParameterList;
private string _statusMessage = "请在左侧选择设备查看 / 编辑配置";
#endregion
public SettingViewModel(IContainerExtension container) : base(container)
@@ -199,6 +205,20 @@ namespace SettingModule.ViewModels
SelectedDevice = DeviceList[0];
}
DeviceList = SystemConfig.DeviceList;
SharedParameterList=SystemConfig.SharedParameterList;
if (SharedParameterList.Count == 0)
{
foreach (var device in SystemConfig.ParameterList)
{
var sharedParam = new SharedParameter
{
Id = device.ID.ToString(),
ParameterName = device.Name,
Value = device.Value is int intVal ? intVal : Convert.ToInt32(device.Value)
};
SharedParameterList.Add(sharedParam);
}
}
IsInitiated = true;
}
}

View File

@@ -462,6 +462,33 @@
</StackPanel>
</Border>
<Border Background="White"
BorderBrush="#E0E0E0" BorderThickness="1"
CornerRadius="4" Padding="14"
Margin="0,0,0,12">
<StackPanel>
<TextBlock Text="公共变量SharedParameterList" FontWeight="Bold" FontSize="14" Margin="0,0,0,10"/>
<TextBlock Foreground="#888" FontSize="12" TextWrapping="Wrap"
Text="每个台架可独立设置自己的通道号等整型变量;新建或打开程序时会用此处值覆盖程序中的同名变量。" Margin="0,0,0,10"/>
<ItemsControl ItemsSource="{Binding SharedParameterList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="0,4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="160"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding ParameterName}" VerticalAlignment="Center"/>
<TextBox Grid.Column="1"
VerticalAlignment="Center"
Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Border>
<Border Background="White"
BorderBrush="#E0E0E0" BorderThickness="1"
CornerRadius="4" Padding="14"

View File

@@ -16,13 +16,6 @@ namespace TestingModule.ViewModels
public class ParametersManagerViewModel:NavigateViewModelBase, IDisposable
{
#region
//private ObservableCollection<DeviceConfigModel> _DeviceList;
//public ObservableCollection<DeviceConfigModel> DeviceList
//{
// get { return _DeviceList; }
// set { SetProperty(ref _DeviceList,value); }
//}
private ObservableCollection<DeviceInfoVM> _DeviceInfoModel;
public ObservableCollection<DeviceInfoVM> DeviceInfoVM
@@ -82,7 +75,6 @@ namespace TestingModule.ViewModels
public ParametersManagerViewModel(IContainerProvider containerProvider) : base(containerProvider)
{
_ScopedContext = containerProvider.Resolve<ScopedContext>();
_systemConfig = containerProvider.Resolve<SystemConfig>();
_deviceManager = containerProvider.Resolve<DeviceManager>();
@@ -96,8 +88,24 @@ namespace TestingModule.ViewModels
ReConnectCommand = new AsyncDelegateCommand(OnReConnect);
CloseCommand = new AsyncDelegateCommand(OnClose);
LoadedCommand = new DelegateCommand(OnLoad);
InitParameters();
}
#region
private void InitParameters()
{
foreach(var item in _systemConfig.ParameterList)
{
var copy=new ParameterVM(item);
var param = _systemConfig.SharedParameterList.FirstOrDefault(x => x.ParameterName == copy.Name);
if (param != null)
{
copy.Value = param.Value;
}
Program.Parameters.Add(copy);
}
}
#region
private void OnLoad()
{
DeviceInfoVM = _systemConfig.DeviceList;

View File

@@ -110,10 +110,7 @@
ItemsSource="{Binding EnumValues}"
SelectedItem="{Binding Parameter.Value}"
Visibility="{Binding Parameter.Type, Converter={StaticResource IsEnumTypeConverter}}" />-->
<CheckBox Margin="10,0"
VerticalAlignment="Bottom"
Content="保存数据"
IsChecked="{Binding Parameter.IsSave}" />
</StackPanel>
</StackPanel>
</ScrollViewer>

View File

@@ -621,10 +621,7 @@ namespace UIShare
{
LoggerHelper.WarnWithNotify(tmp.Item2);
}
//if (currentPara.IsSave && _scopedContext.Program.Parameters.FirstOrDefault(x => x.ID == currentPara.ID) != null)
//{
// _ = SaveDataToDatabase(_scopedContext.Program.ID, currentPara);
//}
}
var returnType = returnValue?.GetType();
if (returnType != null)

View File

@@ -9,6 +9,7 @@ using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UIShare.UIViewModel;
using static UIShare.UIViewModel.ParameterVM;
namespace UIShare.GlobalVariable
{
@@ -27,6 +28,53 @@ namespace UIShare.GlobalVariable
public string DefaultBLFFilePath { get; set; } = "";
public string DefaultDBCFilePath { get; set; } = "";
public ObservableCollection<DeviceInfoVM> DeviceList = new();
public ObservableCollection<SharedParameter> SharedParameterList = new();
[JsonIgnore]
public ObservableCollection<ParameterVM> ParameterList = new()
{
new ParameterVM
{
Category = ParameterCategory.Input,
Type = typeof(int),
Name = "继电器占位1",
Value = 0,
},
new ParameterVM
{
Category = ParameterCategory.Input,
Type = typeof(int),
Name = "继电器占位2",
Value = 1,
},
new ParameterVM
{
Category = ParameterCategory.Input,
Type = typeof(int),
Name = "CAN通道",
Value = 0,
},
new ParameterVM
{
Category = ParameterCategory.Input,
Type = typeof(int),
Name = "示波器通道",
Value = 0,
},
new ParameterVM
{
Category = ParameterCategory.Input,
Type = typeof(int),
Name = "功率分析仪通道1",
Value = 0,
},
new ParameterVM
{
Category = ParameterCategory.Input,
Type = typeof(int),
Name = "功率分析仪通道2",
Value = 0,
},
};
// public ObservableCollection<DeviceInfoVM> DeviceList { get; set; } = new()
//{
// new DeviceInfoVM

View File

@@ -23,10 +23,8 @@ namespace UIShare.UIViewModel
Type = source.Type;
Category = source.Category;
IsUseVar = source.IsUseVar;
IsSave = source.IsSave;
VariableName = source.VariableName;
VariableID = source.VariableID;
IsGlobal = source.IsGlobal;
Value = source.Value;
LowerLimit = source.LowerLimit;
UpperLimit = source.UpperLimit;
@@ -69,12 +67,6 @@ namespace UIShare.UIViewModel
set => SetProperty(ref _category, value);
}
private bool _isGlobal;
public bool IsGlobal
{
get => _isGlobal;
set => SetProperty(ref _isGlobal, value);
}
private object? _value;
public object? Value
@@ -111,12 +103,7 @@ namespace UIShare.UIViewModel
set => SetProperty(ref _isUseVar, value);
}
private bool _isSave;
public bool IsSave
{
get => _isSave;
set => SetProperty(ref _isSave, value);
}
private string? _variableName;
public string? VariableName

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UIShare.UIViewModel
{
public class SharedParameter:BindableBase
{
private string _id;
public string Id
{
get => _id;
set => SetProperty(ref _id, value);
}
private string _parameterName;
public string ParameterName
{
get => _parameterName;
set => SetProperty(ref _parameterName, value);
}
private int _value;
public int Value
{
get => _value;
set => SetProperty(ref _value, value);
}
}
}