UILayoutParser.cs 4.5 KB

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