UiApplication.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Windows.Media.Imaging;
  12. using Aitex.Core.Account;
  13. using Aitex.Core.RT.Event;
  14. using Aitex.Core.RT.Log;
  15. using Aitex.Core.RT.SCCore;
  16. using Aitex.Core.UI.MVVM;
  17. using Aitex.Core.UI.View.Common;
  18. using Aitex.Core.UI.View.Frame;
  19. using Aitex.Core.Util;
  20. using Aitex.Core.Utilities;
  21. using Aitex.Core.WCF;
  22. using Autofac;
  23. using MECF.Framework.UI.Core.Accounts;
  24. namespace MECF.Framework.UI.Core.Applications
  25. {
  26. public class UiApplication : Singleton<UiApplication>
  27. {
  28. public Action<string> CultureChanged;
  29. public string Culture
  30. {
  31. get; private set;
  32. }
  33. public IUiInstance Current
  34. {
  35. get { return _instance; }
  36. }
  37. public event Action OnWindowsLoaded;
  38. private IUiInstance _instance;
  39. private ViewManager _views;
  40. static ThreadExceptionEventHandler ThreadHandler = new ThreadExceptionEventHandler(Application_ThreadException);
  41. //private Guid _clientId = new Guid();
  42. private IContainer container;
  43. private ContainerBuilder containerBuilder;
  44. public ContainerBuilder ContainerBuilder
  45. {
  46. get => containerBuilder;
  47. }
  48. public IContainer Container
  49. {
  50. get => container;
  51. }
  52. protected System.Windows.Application application { get; set; }
  53. public UiApplication()
  54. {
  55. containerBuilder = new ContainerBuilder();
  56. }
  57. public void Initialize(IUiInstance instance)
  58. {
  59. application = System.Windows.Application.Current;
  60. application.DispatcherUnhandledException += OnUnhandledException;
  61. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  62. AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
  63. //Because this is a static event, you must detach your event handlers when your application is disposed, or memory leaks will result.
  64. Application.ThreadException += ThreadHandler;
  65. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  66. _instance = instance;
  67. var asms = AppDomain.CurrentDomain.GetAssemblies().Where(x => !x.GlobalAssemblyCache).ToArray();
  68. containerBuilder.RegisterAssemblyTypes(asms).Where(x =>
  69. {
  70. return typeof(IBaseView).IsAssignableFrom(x);
  71. })
  72. .PublicOnly()
  73. .AsSelf();
  74. containerBuilder.RegisterAssemblyTypes(asms).Where(x =>
  75. {
  76. return typeof(IBaseModel).IsAssignableFrom(x);
  77. })
  78. .PublicOnly()
  79. .As(t => t.GetInterfaces().First(x => x != typeof(IBaseModel) && typeof(IBaseModel).IsAssignableFrom(x)));
  80. Init();
  81. }
  82. protected void OnUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  83. {
  84. if (e.Exception != null)
  85. {
  86. LOG.Write(e.Exception);
  87. if (e.Exception.InnerException != null)
  88. LOG.Write(e.Exception.InnerException);
  89. }
  90. e.Handled = true;
  91. }
  92. static void CurrentDomain_ProcessExit(object sender, EventArgs e)
  93. {
  94. Application.ThreadException -= ThreadHandler;
  95. }
  96. static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
  97. {
  98. ShowMessageDialog(e.Exception);
  99. }
  100. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  101. {
  102. ShowMessageDialog((Exception)e.ExceptionObject);
  103. }
  104. static void ShowMessageDialog(Exception ex)
  105. {
  106. try
  107. {
  108. MessageBox.Show(string.Format("{0} UI Inner Exception, {1}", UiApplication.Instance.Current.SystemName, ex.Message));
  109. }
  110. finally
  111. {
  112. //Environment.Exit(0);
  113. }
  114. }
  115. public void Terminate()
  116. {
  117. EventClient.Instance.Stop();
  118. Singleton<LogManager>.Instance.Terminate();
  119. }
  120. public bool Init()
  121. {
  122. try
  123. {
  124. Singleton<LogManager>.Instance.Initialize();
  125. //EventClient.Instance.Init();
  126. //_event = new UiEvent();
  127. //SystemConfigManager.Instance.Initialize();
  128. //
  129. //SelectCulture(CultureSupported.English);
  130. //WcfClient.Instance.Initialize();
  131. //object language;
  132. //int i = 0;
  133. //do
  134. //{
  135. // language = WCF.Query.GetConfig(SCName.System_Language);
  136. // i++;
  137. // if (i == 100)
  138. // break;
  139. // Thread.Sleep(500);
  140. //} while (language == null);
  141. container = containerBuilder.Build();
  142. _views = new ViewManager()
  143. {
  144. SystemName = _instance.SystemName,
  145. SystemLogo = _instance.MainIcon,
  146. UILayoutFile = _instance.LayoutFile,
  147. MaxSizeShow = _instance.MaxSizeShow,
  148. };
  149. //SetCulture((language != null && (int)(language) == 2) ? "zh-CN" : "en-US");
  150. //try
  151. {
  152. _views.OnMainWindowLoaded += views_OnMainWindowLoaded;
  153. _views.ShowMainWindow(!_instance.EnableAccountModule);
  154. if (_instance.EnableAccountModule)
  155. {
  156. AccountClient.Instance.Service.RegisterViews(_views.GetAllViewList);
  157. if (_views.SystemName == "GonaSorterUI")
  158. {
  159. GonaMainLogin mainLogin = new GonaMainLogin();
  160. if (mainLogin.ShowDialog() == true)
  161. {
  162. //Account account = SorterUiSystem.Instance.WCF.Account.GetAccountInfo(mainLogin.UserName).AccountInfo;
  163. //_views.SetViewPermission(account);
  164. Account account = AccountClient.Instance.Service.GetAccountInfo(mainLogin.textBoxUserName.Text).AccountInfo;
  165. _views.SetViewPermission(account);
  166. _views.MainWindow.Show();
  167. }
  168. }
  169. else
  170. {
  171. MainLogin mainLogin = new MainLogin();
  172. if (Debugger.IsAttached)
  173. {
  174. Account account = AccountClient.Instance.Service.GetAccountInfo("admin").AccountInfo;
  175. _views.SetViewPermission(account);
  176. _views.MainWindow.Show();
  177. return true;
  178. }
  179. if (mainLogin.ShowDialog() == true)
  180. {
  181. //Account account = SorterUiSystem.Instance.WCF.Account.GetAccountInfo(mainLogin.UserName).AccountInfo;
  182. //_views.SetViewPermission(account);
  183. Account account = AccountClient.Instance.Service.GetAccountInfo(mainLogin.textBoxUserName.Text).AccountInfo;
  184. _views.SetViewPermission(account);
  185. _views.MainWindow.Show();
  186. }
  187. }
  188. }
  189. }
  190. }
  191. catch (Exception ex)
  192. {
  193. MessageBox.Show(_instance.SystemName + "Initialize failed, " + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  194. return false;
  195. }
  196. return true;
  197. }
  198. void views_OnMainWindowLoaded()
  199. {
  200. if (OnWindowsLoaded != null)
  201. OnWindowsLoaded();
  202. }
  203. public void SetSelection(string nav, string sub)
  204. {
  205. _views.UpdateSelection(nav, sub);
  206. }
  207. public void Logoff()
  208. {
  209. if (Current.EnableAccountModule)
  210. {
  211. AccountClient.Instance.Service.Logout(AccountClient.Instance.CurrentUser.AccountId);
  212. }
  213. _views.Logoff();
  214. }
  215. public void SetCulture(string culture)
  216. {
  217. SelectCulture(culture);
  218. _views.SetCulture(culture);
  219. Culture = culture;
  220. if (CultureChanged != null)
  221. {
  222. CultureChanged(culture);
  223. }
  224. }
  225. void SelectCulture(string culture)
  226. {
  227. //string culture = language == 2 ? "zh-CN" : "en-US";
  228. //Copy all MergedDictionarys into a auxiliar list.
  229. var dictionaryList = System.Windows.Application.Current.Resources.MergedDictionaries.ToList();
  230. //Search for the specified culture.
  231. string requestedCulture = string.Format(@"pack://application:,,,/Aitex.Sorter.UI;component/Resources/StringResources.{0}.xaml", culture);
  232. var resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString == requestedCulture);
  233. if (resourceDictionary == null)
  234. {
  235. //If not found, select our default language.
  236. requestedCulture = "StringResources.xaml";
  237. resourceDictionary = dictionaryList.
  238. FirstOrDefault(d => d.Source.OriginalString == requestedCulture);
  239. }
  240. //If we have the requested resource, remove it from the list and place at the end.
  241. //Then this language will be our string table to use.
  242. if (resourceDictionary != null)
  243. {
  244. System.Windows.Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
  245. System.Windows.Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
  246. }
  247. //Inform the threads of the new culture.
  248. Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
  249. Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
  250. }
  251. //退出登录,UI界面仍然显示,所有页面改为只读
  252. public void Logout()
  253. {
  254. if (Current.EnableAccountModule)
  255. {
  256. AccountClient.Instance.Service.Logout(AccountClient.Instance.CurrentUser.AccountId);
  257. }
  258. SerializableDictionary<string, ViewPermission> PermissionDictionary = new SerializableDictionary<string, ViewPermission>();
  259. foreach (var pair in AccountClient.Instance.Service.GetAllViewList())
  260. {
  261. PermissionDictionary[pair.Key] = ViewPermission.Readonly;
  262. }
  263. Account account = new Account()
  264. {
  265. AccountId = "",
  266. Role = "",
  267. AccountStatus = true,
  268. Permission = PermissionDictionary
  269. };
  270. AccountClient.Instance.CurrentUser = account;
  271. _views.SetViewPermission(account);
  272. }
  273. public void Relogin()
  274. {
  275. if (_instance.EnableAccountModule)
  276. {
  277. MainLogin mainLogin = new MainLogin();
  278. mainLogin.IsLogOff = true;
  279. if (mainLogin.ShowDialog() == true)
  280. {
  281. Account account = AccountClient.Instance.Service.GetAccountInfo(mainLogin.textBoxUserName.Text).AccountInfo;
  282. _views.SetViewPermission(account);
  283. }
  284. }
  285. }
  286. }
  287. }