ViewManager.cs 9.8 KB

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