CenterView.xaml.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Reflection;
  15. using Aitex.Core.RT.Log;
  16. using MECF.Framework.UI.Core.Applications;
  17. using Autofac;
  18. using Aitex.Core.RT.Device;
  19. using MECF.Framework.Common.Simulator;
  20. namespace Aitex.Core.UI.View.Frame
  21. {
  22. /// <summary>
  23. /// Interaction logic for CenterView.xaml
  24. /// </summary>
  25. public partial class CenterView : UserControl
  26. {
  27. Dictionary<string, CenterTabView> _views = new Dictionary<string,CenterTabView>();
  28. public CenterView()
  29. {
  30. InitializeComponent();
  31. }
  32. public void CreateView(List<ViewItem> views )
  33. {
  34. SimulatorCommManager.Instance.Initialize();
  35. MotorSimulator.Instance.Initialize();
  36. foreach (ViewItem item in views)
  37. {
  38. if (item.SubView == null || item.SubView.Count == 0)
  39. continue;
  40. CenterTabView tab = new CenterTabView();
  41. tab.Tag = item;
  42. foreach (ViewItem subItem in item.SubView)
  43. {
  44. Type t = Assembly.Load(subItem.AssemblyName).GetType(subItem.ViewClass);
  45. if (t == null)
  46. throw new ApplicationException(string.Format("The ui layout config file not valid, can not find {0} at assembly {1}", subItem.ViewClass, subItem.AssemblyName));
  47. UserControl uc;
  48. try
  49. {
  50. using (var scope = UiApplication.Instance.Container.BeginLifetimeScope())
  51. {
  52. if (scope.TryResolve(t, out var target))
  53. {
  54. uc = target as UserControl;
  55. }
  56. else
  57. {
  58. uc = (UserControl)Activator.CreateInstance(t);
  59. }
  60. }
  61. }
  62. catch (Exception ex)
  63. {
  64. LOG.WriteExeption(ex);
  65. throw new ApplicationException(string.Format("Failed to initialize UI window {0}, {1}", subItem.ViewClass, ex.Message));
  66. }
  67. Type tModel = Assembly.Load(subItem.AssemblyName).GetType($"{subItem.ViewClass}Model");
  68. if (!string.IsNullOrEmpty(subItem.Port))
  69. {
  70. uc.DataContext = Activator.CreateInstance(tModel, subItem.Port);
  71. }
  72. tab.Add(subItem, uc);
  73. }
  74. _views[item.Id] = tab;
  75. }
  76. }
  77. public void SetSelection(string id)
  78. {
  79. if (!_views.ContainsKey(id))
  80. return;
  81. _views[id].Height = this.Height ;
  82. gridContent.Children.Clear();
  83. gridContent.Children.Add(_views[id]);
  84. }
  85. public string GetCurrentViewName(string culture)
  86. {
  87. if (gridContent.Children.Count == 0)
  88. return string.Empty;
  89. ViewItem info = (gridContent.Children[0] as CenterTabView).Tag as ViewItem;
  90. if (!string.IsNullOrEmpty(culture) && info.GlobalName.ContainsKey(culture))
  91. return info.GlobalName[culture];
  92. return info.Name;
  93. }
  94. public UserControl GetView(string id)
  95. {
  96. foreach (var item in _views)
  97. {
  98. if (item.Value.FindView(id) != null)
  99. return item.Value.FindView(id);
  100. }
  101. return null;
  102. }
  103. public TabItem GetTab(string id)
  104. {
  105. foreach (var item in _views)
  106. {
  107. if (item.Value.FindTab(id) != null)
  108. return item.Value.FindTab(id);
  109. }
  110. return null;
  111. }
  112. public void SetCulture(string culture)
  113. {
  114. foreach (var item in _views)
  115. {
  116. item.Value.SetCulture(culture);
  117. }
  118. }
  119. }
  120. }