using System; using System.Collections.Generic; using System.Linq; namespace TeamAAS.Vision { /// 视觉画面框描述:唯一 ID + 用户可见名称 public class VisualFrame { /// 唯一标识(如 "home:1"、"inspect:main"),跨页面/插件引用用 public string Id { get; } /// 用户可见名称(如 "主画面"、"上视相机") public string DisplayName { get; } /// 附加信息(页面自用) public object Tag { get; } public VisualFrame(string id, string displayName, object tag = null) { Id = id ?? throw new ArgumentNullException(nameof(id)); DisplayName = displayName ?? id; Tag = tag; } } /// /// 视觉窗体(画面框)管理类:全程序唯一的画面框注册表。 /// 页面按配置创建画面框后在此注册(唯一 ID + 用户可见名称); /// 其他页面/插件通过 ID 拿到画面框(选择结果显示到哪个框、读取框名称等)。 /// 线程安全。 /// public static class VisualFrameManager { private static readonly object _sync = new object(); private static readonly Dictionary _frames = new Dictionary(StringComparer.OrdinalIgnoreCase); /// 画面框注册变化(注册/注销/改名)通知 public static event Action FramesChanged; /// 注册(或更新名称)一个画面框 public static VisualFrame Register(string id, string displayName, object tag = null) { if (string.IsNullOrWhiteSpace(id)) throw new ArgumentException("画面框 ID 不能为空", nameof(id)); VisualFrame frame; lock (_sync) { if (_frames.TryGetValue(id, out var exist)) { frame = new VisualFrame(id, displayName ?? exist.DisplayName, tag ?? exist.Tag); _frames[id] = frame; } else { frame = new VisualFrame(id, displayName, tag); _frames.Add(id, frame); } } FramesChanged?.Invoke(); return frame; } /// 注销画面框 public static bool Unregister(string id) { bool removed; lock (_sync) { removed = _frames.Remove(id); } if (removed) FramesChanged?.Invoke(); return removed; } /// 按 ID 取画面框;不存在返回 null public static VisualFrame GetFrame(string id) { lock (_sync) { return _frames.TryGetValue(id, out var f) ? f : null; } } /// 按 ID 取画面框显示名称;不存在时返回 ID 本身 public static string GetDisplayName(string id) => GetFrame(id)?.DisplayName ?? id; /// 全部已注册画面框(快照) public static IReadOnlyList GetAll() { lock (_sync) { return _frames.Values.ToList(); } } /// 清空全部注册(页面卸载/引擎切换时) public static void Clear() { lock (_sync) { if (_frames.Count == 0) return; _frames.Clear(); FramesChanged?.Invoke(); } } } }