ViewManager.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Windows.Documents;
  8. using System.Windows.Media;
  9. using System.Windows.Controls;
  10. using System.Reflection;
  11. using System.Windows;
  12. using Aitex.Core.Account;
  13. using MECF.Framework.Common.Properties;
  14. using MECF.Framework.UI.Core.Properties;
  15. using Autofac;
  16. using Aitex.Core.UI.View.Common;
  17. using MECF.Framework.UI.Core.Applications;
  18. namespace Aitex.Core.UI.View.Frame
  19. {
  20. public class ViewManager
  21. {
  22. public const string Culture_CN = "zh-CN";
  23. public const string Culture_EN = "en-US";
  24. public List<string> GetAllViewList
  25. {
  26. get { return _views == null ? null : _views.ViewIdList; }
  27. }
  28. public string SystemName { get; set; }
  29. public string UILayoutFile { get; set; }
  30. public string ViewAssembly { get; set; }
  31. public ImageSource SystemLogo { get; set; }
  32. public Window MainWindow { get { return _mainWindow; } }
  33. public event Action OnMainWindowLoaded;
  34. public static Account.Account LoginAccount;
  35. public bool MaxSizeShow { get; set; }
  36. UILayoutParser _views;
  37. ITopView _topView;
  38. BottomView _bottomView = new BottomView();
  39. CenterView _centerView = new CenterView();
  40. StandardFrameWindow _mainWindow;
  41. private bool _isLogoff;
  42. private string _culture = "en-US";
  43. public int PreferWidth { get; set; }
  44. public int PreferHeight { get; set; }
  45. public int PreferHeightTopPanel { get; set; }
  46. public int PreferHeightCenterPanel { get; set; }
  47. public int PreferHeightBottomPanel { get; set; }
  48. public ViewManager()
  49. {
  50. PreferWidth = 1920;
  51. PreferHeight = 1020;
  52. PreferHeightTopPanel = 130;
  53. PreferHeightCenterPanel = 800;
  54. PreferHeightBottomPanel = 90;
  55. MaxSizeShow = true;
  56. }
  57. public void InitWindow()
  58. {
  59. _views = new UILayoutParser(UILayoutFile);
  60. if (_views.PreferWidth > 0)
  61. PreferWidth = _views.PreferWidth;
  62. if (_views.PreferTopPanelHeight > 0)
  63. PreferHeightTopPanel = _views.PreferTopPanelHeight;
  64. if (_views.PreferCenterPanelHeight > 0)
  65. PreferHeightCenterPanel = _views.PreferCenterPanelHeight;
  66. if (_views.PreferBottomPanelHeight > 0)
  67. PreferHeightBottomPanel = _views.PreferBottomPanelHeight;
  68. PreferHeight = PreferHeightTopPanel + PreferHeightCenterPanel + PreferHeightBottomPanel;
  69. try
  70. {
  71. using (var scope = UiApplication.Instance.Container.BeginLifetimeScope())
  72. {
  73. var type = Assembly.Load(_views.TitleView.AssemblyName).GetType(_views.TitleView.ViewClass);
  74. _topView = scope.Resolve(type) as ITopView;
  75. }
  76. }
  77. catch (Exception )
  78. {
  79. throw new ApplicationException(string.Format("在程序集{0}中,没有找到{1},请检查UILayout配置文件中的设置", ViewAssembly, _views.TitleView.ViewClass));
  80. }
  81. UserControl uc = (UserControl)_topView;
  82. _bottomView.CreateMenu(_views.NavigationView);
  83. _bottomView.ButtonClicked += new Action<string>(_bottomView_ButtonClicked);
  84. _centerView.CreateView(_views.NavigationView);
  85. }
  86. void _bottomView_ButtonClicked(string obj)
  87. {
  88. UpdateSelection(obj, "");
  89. }
  90. public void ShowMainWindow(bool visible)
  91. {
  92. InitWindow();
  93. _mainWindow = new StandardFrameWindow()
  94. {
  95. TopView = _topView as UserControl,
  96. BottomView = _bottomView,
  97. CenterView = _centerView,
  98. Icon = SystemLogo,
  99. Title = SystemName,
  100. WindowState = MaxSizeShow ? WindowState.Maximized : WindowState.Normal,
  101. };
  102. _mainWindow.CenterGrid.Width = PreferWidth;
  103. _mainWindow.CenterGrid.Height = PreferHeight;
  104. _mainWindow.TopRow.Height = new GridLength(PreferHeightTopPanel);
  105. _mainWindow.CenterRow.Height = new GridLength(PreferHeightCenterPanel);
  106. _mainWindow.BottomRow.Height = new GridLength(PreferHeightBottomPanel);
  107. _centerView.Height = PreferHeightCenterPanel;
  108. _bottomView.Height = PreferHeightBottomPanel;
  109. _mainWindow.UpdateLayout();
  110. UpdateSelection(_views.NavigationView[0].Id, "");
  111. _mainWindow.Closing += new System.ComponentModel.CancelEventHandler(_mainWindow_Closing);
  112. _mainWindow.Loaded += new RoutedEventHandler(_mainWindow_Loaded);
  113. if (visible)
  114. _mainWindow.Show();
  115. }
  116. public void Logoff()
  117. {
  118. _isLogoff = true;
  119. _mainWindow.Close();
  120. }
  121. void _mainWindow_Loaded(object sender, RoutedEventArgs e)
  122. {
  123. if (OnMainWindowLoaded != null)
  124. OnMainWindowLoaded();
  125. }
  126. void _mainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  127. {
  128. if (_isLogoff)
  129. {
  130. e.Cancel = false;
  131. System.Diagnostics.Process.Start(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
  132. Application.Current.Shutdown();
  133. return;
  134. }
  135. e.Cancel = !Exit();
  136. }
  137. bool Exit()
  138. {
  139. return MessageBox.Show("Are you sure you want to exit system?", SystemName, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes;
  140. }
  141. void UpdateSelection(string navigationId, string subviewId)
  142. {
  143. if (navigationId == "exit")
  144. {
  145. _mainWindow.Close();
  146. return;
  147. }
  148. _bottomView.SetSelection(navigationId);
  149. _centerView.SetSelection(navigationId);
  150. _topView.SetTitle(_centerView.GetCurrentViewName(_culture));
  151. }
  152. public UserControl FindView(string id)
  153. {
  154. return _centerView.GetView(id);
  155. }
  156. public TabItem FindTab(string id)
  157. {
  158. return _centerView.GetTab(id);
  159. }
  160. public void SetViewPermission(Account.Account account)
  161. {
  162. UserControl userControl;
  163. TabItem tabItem;
  164. LoginAccount = account;
  165. foreach (var dic in account.Permission)
  166. {
  167. userControl = FindView(dic.Key);
  168. tabItem = FindTab(dic.Key);
  169. if (userControl == null)
  170. continue;
  171. switch (dic.Value)
  172. {
  173. case ViewPermission.FullyControl:
  174. case ViewPermission.ProcessOPControl:
  175. userControl.Visibility = Visibility.Visible;
  176. userControl.IsEnabled = true;
  177. break;
  178. case ViewPermission.PartlyControl:
  179. userControl.Visibility = Visibility.Visible;
  180. userControl.IsEnabled = true;
  181. break;
  182. case ViewPermission.Readonly:
  183. userControl.Visibility = Visibility.Visible;
  184. userControl.IsEnabled = dic.Key == "recipe" ? true : false;
  185. break;
  186. case ViewPermission.Invisiable:
  187. userControl.Visibility = Visibility.Hidden;
  188. tabItem.Visibility = Visibility.Hidden;
  189. tabItem.Width = 0;
  190. break;
  191. }
  192. }
  193. foreach (ViewItem item in _views.NavigationView)
  194. {
  195. bool enable = false;
  196. foreach (ViewItem sub in item.SubView)
  197. {
  198. foreach (var dic in account.Permission)
  199. {
  200. if (dic.Key == sub.Id && dic.Value != ViewPermission.Invisiable)
  201. {
  202. enable = true;
  203. break;
  204. }
  205. }
  206. }
  207. _bottomView.Enable(item.Id, enable);
  208. }
  209. }
  210. public void SetCulture(string culture)
  211. {
  212. _culture = culture;
  213. if (_topView != null)
  214. _topView.SetTitle(_centerView.GetCurrentViewName(_culture));
  215. _centerView.SetCulture(culture);
  216. _bottomView.SetCulture(culture);
  217. UpdateCultureResource(culture);
  218. }
  219. private void UpdateCultureResource(string culture)
  220. {
  221. //string culture = language == 2 ? "zh-CN" : "en-US";
  222. //Copy all MergedDictionarys into a auxiliar list.
  223. var dictionaryList = Application.Current.Resources.MergedDictionaries.ToList();
  224. //Search for the specified culture.
  225. string requestedCulture = string.Format(@"/MECF.Framework.Common;component/Resources/Language/StringResources.{0}.xaml", culture);
  226. var resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString == requestedCulture);
  227. if (resourceDictionary == null)
  228. {
  229. //If not found, select our default language.
  230. requestedCulture = "StringResources.xaml";
  231. resourceDictionary = dictionaryList.
  232. FirstOrDefault(d => d.Source.OriginalString == requestedCulture);
  233. }
  234. //If we have the requested resource, remove it from the list and place at the end.
  235. //Then this language will be our string table to use.
  236. if (resourceDictionary != null)
  237. {
  238. Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
  239. Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
  240. }
  241. //Inform the threads of the new culture.
  242. Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
  243. Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
  244. }
  245. }
  246. }