UILayoutParser.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Xml;
  7. namespace Aitex.Core.UI.View.Frame
  8. {
  9. public class ViewItem
  10. {
  11. public string Id { get; set; }
  12. public string Name { get; set; }
  13. public string ViewClass { get; set; }
  14. public string AssemblyName { get; set; }
  15. public bool IsSelected { get; set; }
  16. public string Port { get; set; }
  17. public List<ViewItem> SubView { get; set; }
  18. public Dictionary<string, string> GlobalName
  19. {
  20. get; set; }
  21. }
  22. class UILayoutParser
  23. {
  24. public List<string> ViewIdList { get; set; }
  25. public ViewItem TitleView { get; private set; }
  26. public List<ViewItem> NavigationView { get; private set; }
  27. public int PreferTopPanelHeight = -1;
  28. public int PreferCenterPanelHeight = -1;
  29. public int PreferBottomPanelHeight = -1;
  30. public int PreferWidth = -1;
  31. public UILayoutParser(string xmlFile)
  32. {
  33. ViewIdList = new List<string>();
  34. ParseLayout(xmlFile);
  35. }
  36. void ParseLayout(string xmlFile)
  37. {
  38. if (String.IsNullOrEmpty(xmlFile))
  39. throw new ApplicationException("The UI layout config filename is empty,can not initialize UI");
  40. if (!File.Exists(xmlFile))
  41. throw new ApplicationException("Did not find the UI layout config file, " + xmlFile);
  42. XmlDocument doc = new XmlDocument();
  43. try
  44. {
  45. doc.Load(xmlFile);
  46. var node = doc.SelectSingleNode("/MECFUI") as XmlNode;
  47. int parsed = -1;
  48. if (node.Attributes["TopPanelHeight"] != null && int.TryParse(node.Attributes["TopPanelHeight"].Value, out parsed))
  49. {
  50. PreferTopPanelHeight = parsed;
  51. }
  52. if (node.Attributes["CenterPanelHeight"] != null && int.TryParse(node.Attributes["CenterPanelHeight"].Value, out parsed))
  53. {
  54. PreferCenterPanelHeight = parsed;
  55. }
  56. if (node.Attributes["BottomPanelHeight"] != null && int.TryParse(node.Attributes["BottomPanelHeight"].Value, out parsed))
  57. {
  58. PreferBottomPanelHeight = parsed;
  59. }
  60. if (node.Attributes["Width"] != null && int.TryParse(node.Attributes["Width"].Value, out parsed))
  61. {
  62. PreferWidth = parsed;
  63. }
  64. }
  65. catch (Exception ex)
  66. {
  67. throw new ApplicationException("UI layout config file " + xmlFile + " is not valid, " + ex.Message);
  68. }
  69. TitleView = ParseNode(doc.SelectSingleNode("/MECFUI/Title"));
  70. foreach (XmlNode nav in doc.SelectNodes("/MECFUI/Navigation"))
  71. {
  72. ViewItem menu = ParseNode(nav);
  73. if (menu == null)
  74. continue;
  75. foreach (XmlNode ele in nav.SelectNodes("SubView"))
  76. {
  77. if (menu.SubView == null)
  78. menu.SubView = new List<ViewItem>();
  79. ViewItem item = ParseNode(ele);
  80. if (item != null)
  81. {
  82. menu.SubView.Add(item);
  83. ViewIdList.Add(item.Id);
  84. }
  85. }
  86. if (NavigationView == null)
  87. NavigationView = new List<ViewItem>();
  88. NavigationView.Add(menu);
  89. }
  90. }
  91. ViewItem ParseNode(XmlNode node)
  92. {
  93. if (node == null)
  94. return null;
  95. ViewItem item = new ViewItem();
  96. item.GlobalName = new Dictionary<string, string>();
  97. if (node.Attributes["Id"] != null)
  98. item.Id = node.Attributes["Id"].Value;
  99. if (node.Attributes["Name"] != null)
  100. item.Name = node.Attributes["Name"].Value;
  101. if (node.Attributes["Name.en-US"] != null)
  102. item.GlobalName["en-US"] = node.Attributes["Name.en-US"].Value;
  103. if (node.Attributes["Name.zh-CN"] != null)
  104. item.GlobalName["zh-CN"] = node.Attributes["Name.zh-CN"].Value;
  105. if (node.Attributes["ViewClass"] != null)
  106. item.ViewClass = node.Attributes["ViewClass"].Value;
  107. if (node.Attributes["Assembly"] != null)
  108. item.AssemblyName = node.Attributes["Assembly"].Value;
  109. if (node.Attributes["Port"] != null)
  110. item.Port = node.Attributes["Port"].Value;
  111. return item;
  112. }
  113. }
  114. }