125 lines
5.0 KiB
C#
125 lines
5.0 KiB
C#
using Check.Main.Camera;
|
||
using Check.Main.Dispatch;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Xml.Serialization;
|
||
|
||
namespace Check.Main.Common
|
||
{
|
||
/// <summary>
|
||
/// 静态全局配置管理器,作为配置数据的“单一数据源”。
|
||
/// 负责加载、保存和通知配置变更。
|
||
/// </summary>
|
||
public static class ConfigurationManager
|
||
{
|
||
// private static readonly string _configFilePath = Path.Combine(Application.StartupPath, "main_config.xml");
|
||
// private static ProcessConfig _currentConfig;
|
||
// private static readonly object _lock = new object();
|
||
|
||
// /// <summary>
|
||
// /// 当配置通过 SaveChanges() 方法保存后触发。
|
||
// /// 其他模块(如 FormControlPanel)可以订阅此事件以响应配置变更。
|
||
// /// </summary>
|
||
// public static event Action OnConfigurationChanged;
|
||
|
||
// /// <summary>
|
||
// /// 静态构造函数在类首次被访问时自动执行,确保配置只被加载一次。
|
||
// /// </summary>
|
||
// static ConfigurationManager()
|
||
// {
|
||
// Load();
|
||
// }
|
||
|
||
// /// <summary>
|
||
// /// 获取对当前配置对象的直接引用。
|
||
// /// PropertyGrid 可以直接绑定到这个对象进行编辑。
|
||
// /// </summary>
|
||
// /// <returns>当前的 ProcessConfig 实例。</returns>
|
||
// public static ProcessConfig GetCurrentConfig()
|
||
// {
|
||
// return _currentConfig;
|
||
// }
|
||
|
||
// /// <summary>
|
||
// /// 从 XML 文件加载配置。如果文件不存在或加载失败,则创建一个新的默认配置。
|
||
// /// </summary>
|
||
// private static void Load()
|
||
// {
|
||
// lock (_lock)
|
||
// {
|
||
// if (File.Exists(_configFilePath))
|
||
// {
|
||
// try
|
||
// {
|
||
// XmlSerializer serializer = new XmlSerializer(typeof(ProcessConfig));
|
||
// using (var fs = new FileStream(_configFilePath, FileMode.Open, FileAccess.Read))
|
||
// {
|
||
// _currentConfig = (ProcessConfig)serializer.Deserialize(fs);
|
||
// ThreadSafeLogger.Log("主配置文件加载成功。");
|
||
// }
|
||
// }
|
||
// catch (Exception ex)
|
||
// {
|
||
// ThreadSafeLogger.Log("加载主配置失败: " + ex.Message + " 将使用默认配置。");
|
||
// _currentConfig = new ProcessConfig();
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// ThreadSafeLogger.Log("未找到主配置文件,将创建新的默认配置。");
|
||
// _currentConfig = new ProcessConfig();
|
||
// }
|
||
// }
|
||
// }
|
||
|
||
// /// <summary>
|
||
// /// 将当前配置保存到文件,并通知所有监听者配置已更改。
|
||
// /// 这是响应UI变化的推荐方法。
|
||
// /// </summary>
|
||
// public static void SaveChanges()
|
||
// {
|
||
// lock (_lock)
|
||
// {
|
||
// try
|
||
// {
|
||
// XmlSerializer serializer = new XmlSerializer(typeof(ProcessConfig));
|
||
// using (var fs = new FileStream(_configFilePath, FileMode.Create, FileAccess.Write))
|
||
// {
|
||
// serializer.Serialize(fs, _currentConfig);
|
||
// }
|
||
// }
|
||
// catch (Exception ex)
|
||
// {
|
||
// ThreadSafeLogger.Log("保存主配置失败: " + ex.Message);
|
||
// }
|
||
// }
|
||
|
||
// // 在锁之外触发事件,以避免监听者中的代码导致死锁
|
||
// ThreadSafeLogger.Log("配置已保存,正在触发 OnConfigurationChanged 事件...");
|
||
// OnConfigurationChanged?.Invoke();
|
||
// }
|
||
// 不再自己管理配置,而是直接从 ProductManager 获取
|
||
public static ProcessConfig GetCurrentConfig()
|
||
{
|
||
return ProductManager.CurrentConfig;
|
||
}
|
||
|
||
public static void SaveChanges()
|
||
{
|
||
ProductManager.SaveCurrentProductConfig();
|
||
}
|
||
|
||
// OnConfigurationChanged 事件现在由 ProductManager.OnProductChanged 替代
|
||
// 如果其他地方还依赖这个事件,可以做一个桥接:
|
||
public static event Action OnConfigurationChanged
|
||
{
|
||
add { ProductManager.OnProductChanged += value; }
|
||
remove { ProductManager.OnProductChanged -= value; }
|
||
}
|
||
}
|
||
|
||
}
|