默认文件路径保存

This commit is contained in:
hsc
2026-06-10 13:08:52 +08:00
parent c01aa6e545
commit 59d047d8e6
5 changed files with 54 additions and 19 deletions

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 ?? "运行";
@@ -179,7 +180,7 @@ namespace ADP.ViewModels
Application.Current.MainWindow.Show();
_regionManager.RequestNavigate("ShellViewManager", "MainView");
});
_eventAggregator.GetEvent<RunSingalCompletedEvent>().Subscribe(UpdateRunIcon);
_globalInfo.ScopeChanged += (s, e) =>
@@ -459,6 +460,7 @@ namespace ADP.ViewModels
private void SetDefault()
{
if (CurrentConfig == null) return;
// 💡 1. 抓取触发瞬间的 Scope 与 Context 快照
string runningScope = _globalInfo.CurrentScope;
ScopedContext? targetContext = CurrentContext;
@@ -467,8 +469,8 @@ namespace ADP.ViewModels
if (targetContext.CurrentFilePath != null)
{
//SystemConfig.DefaultProgramFilePath = targetContext.CurrentFilePath;
//ConfigService.Save();
CurrentConfig.DefaultProgramFilePath = targetContext.CurrentFilePath;
ConfigService.Save(CurrentConfig);
LoggerHelper.SuccessWithNotify($"工位 [{runningScope}] 已成功将当前程序设为默认启动程序");
}
}

View File

@@ -60,11 +60,11 @@
CommandParameter="{Binding Content, RelativeSource={RelativeSource Self}}"
Style="{StaticResource MaterialDesignFlatButton}"
Margin="8" />
<Button Content="更新界面"
<!--<Button Content="更新界面"
Command="{Binding NavigateCommand}"
CommandParameter="{Binding Content, RelativeSource={RelativeSource Self}}"
Style="{StaticResource MaterialDesignFlatButton}"
Margin="8" />
Margin="8" />-->
</StackPanel>
</materialDesign:DrawerHost.LeftDrawerContent>
<Grid>

View File

@@ -1,10 +1,13 @@
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;
namespace MainModule.ViewModels
@@ -50,6 +53,7 @@ namespace MainModule.ViewModels
// 在该作用域内解析 ScopedContext —— 当前作用域唯一
_scopedContext = _scope.Resolve<ScopedContext>();
_stepRunning = _scope.Resolve<StepRunning>();
_systemConfig = _scope.Resolve<SystemConfig>();
// 关键:从同一个 _scope 解析 5 个子 VMDI 会把同一个 ScopedContext 注入它们
CommandTreeVM = _scope.Resolve<CommandTreeViewModel>();
StepsManagerVM = _scope.Resolve<StepsManagerViewModel>();
@@ -71,6 +75,7 @@ namespace MainModule.ViewModels
{
_globalInfo.ContextDic?.Remove(TestStatus);
_globalInfo.StepRunningDic?.Remove(TestStatus);
_globalInfo.ConfigDic?.Remove(TestStatus);
}
catch (Exception ex)
{
@@ -104,10 +109,6 @@ namespace MainModule.ViewModels
if (navigationContext.Parameters.ContainsKey("Name"))
{
TestStatus = navigationContext.Parameters.GetValue<string>("Name");
_globalInfo.ContextDic.Add(TestStatus, _scopedContext);
_globalInfo.StepRunningDic.Add(TestStatus, _stepRunning);
_globalInfo.ScopeDic.Add(TestStatus, _scope);
_systemConfig = _scope.Resolve<SystemConfig>();
if (ConfigService.IsExit(TestStatus))
{
string filePath = System.IO.Path.Combine(_systemConfig.SystemPath, $"{TestStatus}.json");
@@ -119,6 +120,31 @@ namespace MainModule.ViewModels
Newtonsoft.Json.JsonConvert.PopulateObject(json, _systemConfig);
}
}
_globalInfo.ContextDic.Add(TestStatus, _scopedContext);
_globalInfo.StepRunningDic.Add(TestStatus, _stepRunning);
_globalInfo.ScopeDic.Add(TestStatus, _scope);
_globalInfo.ConfigDic.Add(TestStatus, _systemConfig);
if(_systemConfig.DefaultProgramFilePath != null&&File.Exists(_systemConfig.DefaultProgramFilePath))
{
var filePath = _systemConfig.DefaultProgramFilePath;
// 读取 JSON 文件
string json = File.ReadAllText(filePath);
// 反序列化为 ProgramModel
var program = Newtonsoft.Json.JsonConvert.DeserializeObject<ProgramModel>(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

@@ -15,8 +15,13 @@ 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;
@@ -61,7 +66,7 @@ namespace SettingModule.ViewModels
#endregion
#region
private IScopedProvider _scope;
private SystemConfig _systemConfig;
private ScopedContext _scopedContext { get; set; }
private GlobalInfo _globalInfo { get; }
private bool IsInitiated = false;
@@ -110,20 +115,20 @@ 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>
@@ -164,7 +169,7 @@ namespace SettingModule.ViewModels
var p = new DialogParameters
{
{ "Device", SelectedDevice },
{ "SystemConfig", _systemConfig }
{ "SystemConfig", SystemConfig }
};
_dialogService.ShowDialog(dialogName, p, r =>
@@ -186,12 +191,12 @@ 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;
}
}

View File

@@ -11,6 +11,7 @@ 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;
@@ -31,6 +32,7 @@ namespace UIShare.GlobalVariable
{
ContextDic = new();
StepRunningDic = new();
ConfigDic = new();
ScopeDic = new();
CurrentScope = "default";
}