UILayoutParser.cs 4.9 KB

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