FrameNoConverter.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using TeamAAS.FlowEditor;
  5. using TeamAAS.FlowEditor.ProductModels;
  6. namespace Plugins.Vpp.Core
  7. {
  8. /// <summary>
  9. /// 「目标画面」名称下拉转换器:下拉项来自当前编辑产品的「画面列表」(ProductMeta.HomeFrames),
  10. /// 显示画面名称;选择名称后由 <see cref="Models.VppDisplayItem.FrameName"/> 映射回画面序号存储。
  11. /// 产品没有配置画面时允许手动输入名称/数字兜底。
  12. /// </summary>
  13. public class FrameNameConverter : StringConverter
  14. {
  15. /// <summary>当前编辑产品的画面列表(读不到返回空列表)</summary>
  16. internal static List<HomeFrameItem> GetFrames()
  17. {
  18. try
  19. {
  20. var pm = ProductManager.Instance;
  21. var cfg = pm.GetHomeFrameConfig(pm.CurrentProductName);
  22. return cfg?.HomeFrames ?? new List<HomeFrameItem>();
  23. }
  24. catch
  25. {
  26. return new List<HomeFrameItem>();
  27. }
  28. }
  29. /// <summary>第 no 个画面的显示名称(越界/空名时补"画面N")</summary>
  30. internal static string GetFrameName(List<HomeFrameItem> frames, int no)
  31. {
  32. if (frames != null && no >= 1 && no <= frames.Count)
  33. {
  34. var name = frames[no - 1]?.Name;
  35. if (!string.IsNullOrWhiteSpace(name)) return name;
  36. }
  37. return $"画面{no}";
  38. }
  39. /// <summary>画面名称 → 序号(重名取第一个;"画面N"格式兜底;纯数字直接解析;找不到返回 0)</summary>
  40. internal static int ResolveFrameNo(string name)
  41. {
  42. if (string.IsNullOrWhiteSpace(name)) return 0;
  43. name = name.Trim();
  44. var frames = GetFrames();
  45. for (int i = 0; i < frames.Count; i++)
  46. {
  47. if (string.Equals((frames[i]?.Name ?? "").Trim(), name, StringComparison.Ordinal))
  48. return i + 1;
  49. }
  50. if (int.TryParse(name, out int no)) return no;
  51. if (name.StartsWith("画面") && int.TryParse(name.Substring(2), out no)) return no;
  52. return 0;
  53. }
  54. public override bool GetStandardValuesSupported(ITypeDescriptorContext context) => true;
  55. // 有画面列表时排他(只能从下拉选),没有时允许手动输入
  56. public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) => GetFrames().Count > 0;
  57. public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  58. {
  59. var frames = GetFrames();
  60. var names = new string[frames.Count];
  61. for (int i = 0; i < frames.Count; i++) names[i] = GetFrameName(frames, i + 1);
  62. return new StandardValuesCollection(names);
  63. }
  64. }
  65. }