UiApplication.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. using System.Windows.Media.Imaging;
  10. using Aitex.Core.Account;
  11. using Aitex.Core.RT.Event;
  12. using Aitex.Core.RT.Log;
  13. using Aitex.Core.RT.SCCore;
  14. using Aitex.Core.UI.MVVM;
  15. using Aitex.Core.UI.View.Common;
  16. using Aitex.Core.UI.View.Frame;
  17. using Aitex.Core.Util;
  18. using Aitex.Core.Utilities;
  19. using Aitex.Core.WCF;
  20. using Autofac;
  21. using MECF.Framework.UI.Core.Accounts;
  22. namespace MECF.Framework.UI.Core.Applications
  23. {
  24. public class UiApplication : Singleton<UiApplication>
  25. {
  26. public IUiInstance Current
  27. {
  28. get { return _instance; }
  29. }
  30. public event Action OnWindowsLoaded;
  31. private IUiInstance _instance;
  32. private ViewManager _views;
  33. static ThreadExceptionEventHandler ThreadHandler = new ThreadExceptionEventHandler(Application_ThreadException);
  34. //private Guid _clientId = new Guid();
  35. private IContainer container;
  36. private ContainerBuilder containerBuilder;
  37. public ContainerBuilder ContainerBuilder
  38. {
  39. get => containerBuilder;
  40. }
  41. public IContainer Container
  42. {
  43. get => container;
  44. }
  45. public UiApplication()
  46. {
  47. containerBuilder = new ContainerBuilder();
  48. }
  49. public void Initialize(IUiInstance instance)
  50. {
  51. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  52. AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
  53. //Because this is a static event, you must detach your event handlers when your application is disposed, or memory leaks will result.
  54. Application.ThreadException += ThreadHandler;
  55. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  56. _instance = instance;
  57. var asms = AppDomain.CurrentDomain.GetAssemblies().Where(x => !x.GlobalAssemblyCache).ToArray();
  58. containerBuilder.RegisterAssemblyTypes(asms).Where(x =>
  59. {
  60. return typeof(IBaseView).IsAssignableFrom(x);
  61. })
  62. .PublicOnly()
  63. .AsSelf();
  64. containerBuilder.RegisterAssemblyTypes(asms).Where(x =>
  65. {
  66. return typeof(IBaseModel).IsAssignableFrom(x);
  67. })
  68. .PublicOnly()
  69. .As(t => t.GetInterfaces().First(x => x != typeof(IBaseModel) && typeof(IBaseModel).IsAssignableFrom(x)));
  70. Init();
  71. }
  72. static void CurrentDomain_ProcessExit(object sender, EventArgs e)
  73. {
  74. Application.ThreadException -= ThreadHandler;
  75. }
  76. static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
  77. {
  78. ShowMessageDialog(e.Exception);
  79. }
  80. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  81. {
  82. ShowMessageDialog((Exception)e.ExceptionObject);
  83. }
  84. static void ShowMessageDialog(Exception ex)
  85. {
  86. try
  87. {
  88. MessageBox.Show(string.Format("{0} UI Inner Exception, {1}", UiApplication.Instance.Current.SystemName, ex.Message));
  89. }
  90. finally
  91. {
  92. //Environment.Exit(0);
  93. }
  94. }
  95. public void Terminate()
  96. {
  97. EventClient.Instance.Stop();
  98. Singleton<LogManager>.Instance.Terminate();
  99. }
  100. public bool Init()
  101. {
  102. try
  103. {
  104. Singleton<LogManager>.Instance.Initialize();
  105. //EventClient.Instance.Init();
  106. //_event = new UiEvent();
  107. //SystemConfigManager.Instance.Initialize();
  108. //
  109. //SelectCulture(CultureSupported.English);
  110. //WcfClient.Instance.Initialize();
  111. //object language;
  112. //int i = 0;
  113. //do
  114. //{
  115. // language = WCF.Query.GetConfig(SCName.System_Language);
  116. // i++;
  117. // if (i == 100)
  118. // break;
  119. // Thread.Sleep(500);
  120. //} while (language == null);
  121. container = containerBuilder.Build();
  122. _views = new ViewManager()
  123. {
  124. SystemName = _instance.SystemName,
  125. SystemLogo = _instance.MainIcon,
  126. UILayoutFile = _instance.LayoutFile,
  127. MaxSizeShow = _instance.MaxSizeShow,
  128. };
  129. //SetCulture((language != null && (int)(language) == 2) ? "zh-CN" : "en-US");
  130. //try
  131. {
  132. _views.OnMainWindowLoaded += views_OnMainWindowLoaded;
  133. _views.ShowMainWindow(!_instance.EnableAccountModule);
  134. if (_instance.EnableAccountModule)
  135. {
  136. AccountClient.Instance.Service.RegisterViews(_views.GetAllViewList);
  137. MainLogin mainLogin = new MainLogin();
  138. if (mainLogin.ShowDialog() == true)
  139. {
  140. //Account account = SorterUiSystem.Instance.WCF.Account.GetAccountInfo(mainLogin.UserName).AccountInfo;
  141. //_views.SetViewPermission(account);
  142. Account account = AccountClient.Instance.Service.GetAccountInfo(mainLogin.textBoxUserName.Text).AccountInfo;
  143. _views.SetViewPermission(account);
  144. _views.MainWindow.Show();
  145. }
  146. }
  147. }
  148. }
  149. catch (Exception ex)
  150. {
  151. MessageBox.Show(_instance.SystemName + "Initialize failed, " + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  152. return false;
  153. }
  154. return true;
  155. }
  156. void views_OnMainWindowLoaded()
  157. {
  158. if (OnWindowsLoaded != null)
  159. OnWindowsLoaded();
  160. }
  161. public void Logoff()
  162. {
  163. if (Current.EnableAccountModule)
  164. {
  165. AccountClient.Instance.Service.Logout(AccountClient.Instance.CurrentUser.AccountId);
  166. }
  167. _views.Logoff();
  168. }
  169. }
  170. }