CenterView.xaml.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. namespace Aitex.Core.UI.View.Frame
  17. {
  18. /// <summary>
  19. /// Interaction logic for CenterView.xaml
  20. /// </summary>
  21. public partial class CenterView : UserControl
  22. {
  23. Dictionary<string, CenterTabView> _views = new Dictionary<string,CenterTabView>();
  24. public CenterView()
  25. {
  26. InitializeComponent();
  27. }
  28. public void CreateView(List<ViewItem> views, string assemblyName)
  29. {
  30. foreach (ViewItem item in views)
  31. {
  32. if (item.SubView == null || item.SubView.Count == 0)
  33. continue;
  34. CenterTabView tab = new CenterTabView();
  35. tab.Tag = item;
  36. foreach (ViewItem subItem in item.SubView)
  37. {
  38. Type t = Assembly.Load(assemblyName).GetType(subItem.ViewClass);
  39. if (t == null)
  40. t = Assembly.GetExecutingAssembly().GetType(subItem.ViewClass);
  41. if (t == null)
  42. throw new ApplicationException(string.Format("在程序集{0}中,没有找到{1},请检查UILayout配置文件中的设置", assemblyName, subItem.ViewClass));
  43. UserControl uc;
  44. try
  45. {
  46. uc = (UserControl)Activator.CreateInstance(t);
  47. }
  48. catch (Exception ex)
  49. {
  50. LOG.Write(ex);
  51. throw new ApplicationException(string.Format("窗口{0}初始化失败,错误信息:{1}", subItem.ViewClass, ex.Message));
  52. }
  53. if (!string.IsNullOrEmpty(subItem.ViewModelClass))
  54. {
  55. Type vmType = Assembly.Load(assemblyName).GetType(subItem.ViewModelClass);
  56. if (vmType == null)
  57. vmType = Assembly.GetExecutingAssembly().GetType(subItem.ViewModelClass);
  58. if (vmType == null)
  59. throw new ApplicationException(string.Format("没有找到UILayout配置文件中的{0} in {1}", subItem.ViewModelClass, assemblyName));
  60. uc.DataContext = Activator.CreateInstance(vmType);
  61. }
  62. tab.Add(subItem, uc);
  63. }
  64. _views[item.Id] = tab;
  65. }
  66. }
  67. public void SetSelection(string id)
  68. {
  69. if (!_views.ContainsKey(id))
  70. return;
  71. gridContent.Children.Clear();
  72. gridContent.Children.Add(_views[id]);
  73. }
  74. public string GetCurrentViewName(string culture)
  75. {
  76. if (gridContent.Children.Count == 0)
  77. return string.Empty;
  78. ViewItem info = (gridContent.Children[0] as CenterTabView).Tag as ViewItem;
  79. if (!string.IsNullOrEmpty(culture) && info.GlobalName.ContainsKey(culture))
  80. return info.GlobalName[culture];
  81. return info.Name;
  82. }
  83. public UserControl GetView(string id)
  84. {
  85. foreach (var item in _views)
  86. {
  87. if (item.Value.FindView(id) != null)
  88. return item.Value.FindView(id);
  89. }
  90. return null;
  91. }
  92. public TabItem GetTab(string id)
  93. {
  94. foreach (var item in _views)
  95. {
  96. if (item.Value.FindTab(id) != null)
  97. return item.Value.FindTab(id);
  98. }
  99. return null;
  100. }
  101. public void SetCulture(string culture)
  102. {
  103. foreach (var item in _views)
  104. {
  105. item.Value.SetCulture(culture);
  106. }
  107. }
  108. }
  109. }