加载配置文件优化

This commit is contained in:
“hsc”
2026-07-30 13:24:12 +08:00
parent 252c70ef05
commit 0e9f65ea21
3 changed files with 11 additions and 53 deletions

View File

@@ -17,14 +17,14 @@ namespace MainModule
regionManager.RegisterViewWithRegion("ShellViewManager", typeof(MainView));
// 2. 给 3 个台架分别注入测试视图
// 当你点击 "台架 1" 的 Tab 时,就会显示这个 AutomatedTestingView
regionManager.RegisterViewWithRegion("TestCell1", typeof(AutomatedTestingView));
// 当你点击 "台架 1" 的 Tab 时,就会显示这个 ProtocolStartView
regionManager.RegisterViewWithRegion("TestCell1", typeof(ProtocolStartView));
// 当你点击 "台架 2" 的 Tab 时,就会显示这个 AutomatedTestingView
regionManager.RegisterViewWithRegion("TestCell2", typeof(AutomatedTestingView));
// 当你点击 "台架 2" 的 Tab 时,就会显示这个 ProtocolStartView
regionManager.RegisterViewWithRegion("TestCell2", typeof(ProtocolStartView));
// 当你点击 "台架 3" 的 Tab 时,就会显示这个 AutomatedTestingView
regionManager.RegisterViewWithRegion("TestCell3", typeof(AutomatedTestingView));
// 当你点击 "台架 3" 的 Tab 时,就会显示这个 ProtocolStartView
regionManager.RegisterViewWithRegion("TestCell3", typeof(ProtocolStartView));
}
public void RegisterTypes(IContainerRegistry containerRegistry)

View File

@@ -69,6 +69,10 @@ namespace MainModule.ViewModels
string json = System.IO.File.ReadAllText(filePath);
Newtonsoft.Json.JsonConvert.PopulateObject(json, _systemConfig);
}
else
{
_systemConfig = new SystemConfig();
}
}
//容器解析顺序不要改变!!!
_scopedContext = _scope.Resolve<ScopedContext>();

View File

@@ -29,53 +29,7 @@ namespace UIShare.GlobalVariable
}
return true;
}
/// <summary>
/// 根据标题(格子标识)加载独立的配置文件
/// </summary>
public static SystemConfig Load(string title)
{
if (string.IsNullOrEmpty(title))
{
throw new ArgumentException("配置标题不能为空", nameof(title));
}
// 临时实例化一个对象以获取默认的 SystemPath
var dummy = new SystemConfig();
string configPath = Path.Combine(dummy.SystemPath, $"{title}.json");
if (!File.Exists(configPath))
{
// 如果不存在,创建一个带 Title 的默认配置并保存
var defaultConfig = new SystemConfig { Title = title };
EnsureDefaultCanDevice(defaultConfig);
Save(defaultConfig);
return defaultConfig;
}
lock (_fileLock)
{
try
{
string json = File.ReadAllText(configPath);
var config = JsonConvert.DeserializeObject<SystemConfig>(json, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
config ??= new SystemConfig { Title = title };
EnsureDefaultCanDevice(config);
return config;
}
catch (Exception ex)
{
LoggerHelper.ErrorWithNotify(title, $"格子 [{title}] 配置加载失败: {ex.Message}");
var fallback = new SystemConfig { Title = title };
EnsureDefaultCanDevice(fallback);
return fallback;
}
}
}
/// <summary>
/// 保存指定的配置实例
/// </summary>