CenterView.xaml.cs 4.1 KB

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