UiApplication.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. protected System.Windows.Application application { get; set; }
  46. public UiApplication()
  47. {
  48. containerBuilder = new ContainerBuilder();
  49. }
  50. public void Initialize(IUiInstance instance)
  51. {
  52. application = System.Windows.Application.Current;
  53. application.DispatcherUnhandledException += OnUnhandledException;
  54. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  55. AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
  56. //Because this is a static event, you must detach your event handlers when your application is disposed, or memory leaks will result.
  57. Application.ThreadException += ThreadHandler;
  58. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  59. _instance = instance;
  60. var asms = AppDomain.CurrentDomain.GetAssemblies().Where(x => !x.GlobalAssemblyCache).ToArray();
  61. containerBuilder.RegisterAssemblyTypes(asms).Where(x =>
  62. {
  63. return typeof(IBaseView).IsAssignableFrom(x);
  64. })
  65. .PublicOnly()
  66. .AsSelf();
  67. containerBuilder.RegisterAssemblyTypes(asms).Where(x =>
  68. {
  69. return typeof(IBaseModel).IsAssignableFrom(x);
  70. })
  71. .PublicOnly()
  72. .As(t => t.GetInterfaces().First(x => x != typeof(IBaseModel) && typeof(IBaseModel).IsAssignableFrom(x)));
  73. Init();
  74. }
  75. protected void OnUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  76. {
  77. if (e.Exception != null)
  78. {
  79. LOG.Write(e.Exception);
  80. if (e.Exception.InnerException != null)
  81. LOG.Write(e.Exception.InnerException);
  82. }
  83. e.Handled = true;
  84. }
  85. static void CurrentDomain_ProcessExit(object sender, EventArgs e)
  86. {
  87. Application.ThreadException -= ThreadHandler;
  88. }
  89. static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
  90. {
  91. ShowMessageDialog(e.Exception);
  92. }
  93. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  94. {
  95. ShowMessageDialog((Exception)e.ExceptionObject);
  96. }
  97. static void ShowMessageDialog(Exception ex)
  98. {
  99. try
  100. {
  101. MessageBox.Show(string.Format("{0} UI Inner Exception, {1}", UiApplication.Instance.Current.SystemName, ex.Message));
  102. }
  103. finally
  104. {
  105. //Environment.Exit(0);
  106. }
  107. }
  108. public void Terminate()
  109. {
  110. EventClient.Instance.Stop();
  111. Singleton<LogManager>.Instance.Terminate();
  112. }
  113. public bool Init()
  114. {
  115. try
  116. {
  117. Singleton<LogManager>.Instance.Initialize();
  118. //EventClient.Instance.Init();
  119. //_event = new UiEvent();
  120. //SystemConfigManager.Instance.Initialize();
  121. //
  122. //SelectCulture(CultureSupported.English);
  123. //WcfClient.Instance.Initialize();
  124. //object language;
  125. //int i = 0;
  126. //do
  127. //{
  128. // language = WCF.Query.GetConfig(SCName.System_Language);
  129. // i++;
  130. // if (i == 100)
  131. // break;
  132. // Thread.Sleep(500);
  133. //} while (language == null);
  134. container = containerBuilder.Build();
  135. _views = new ViewManager()
  136. {
  137. SystemName = _instance.SystemName,
  138. SystemLogo = _instance.MainIcon,
  139. UILayoutFile = _instance.LayoutFile,
  140. MaxSizeShow = _instance.MaxSizeShow,
  141. };
  142. //SetCulture((language != null && (int)(language) == 2) ? "zh-CN" : "en-US");
  143. //try
  144. {
  145. _views.OnMainWindowLoaded += views_OnMainWindowLoaded;
  146. _views.ShowMainWindow(!_instance.EnableAccountModule);
  147. if (_instance.EnableAccountModule)
  148. {
  149. AccountClient.Instance.Service.RegisterViews(_views.GetAllViewList);
  150. if (_views.SystemName == "GonaSorterUI")
  151. {
  152. GonaMainLogin mainLogin = new GonaMainLogin();
  153. if (mainLogin.ShowDialog() == true)
  154. {
  155. //Account account = SorterUiSystem.Instance.WCF.Account.GetAccountInfo(mainLogin.UserName).AccountInfo;
  156. //_views.SetViewPermission(account);
  157. Account account = AccountClient.Instance.Service.GetAccountInfo(mainLogin.textBoxUserName.Text).AccountInfo;
  158. _views.SetViewPermission(account);
  159. _views.MainWindow.Show();
  160. }
  161. }
  162. else
  163. {
  164. MainLogin mainLogin = new MainLogin();
  165. if (mainLogin.ShowDialog() == true)
  166. {
  167. //Account account = SorterUiSystem.Instance.WCF.Account.GetAccountInfo(mainLogin.UserName).AccountInfo;
  168. //_views.SetViewPermission(account);
  169. Account account = AccountClient.Instance.Service.GetAccountInfo(mainLogin.textBoxUserName.Text).AccountInfo;
  170. _views.SetViewPermission(account);
  171. _views.MainWindow.Show();
  172. }
  173. }
  174. }
  175. }
  176. }
  177. catch (Exception ex)
  178. {
  179. MessageBox.Show(_instance.SystemName + "Initialize failed, " + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  180. return false;
  181. }
  182. return true;
  183. }
  184. void views_OnMainWindowLoaded()
  185. {
  186. if (OnWindowsLoaded != null)
  187. OnWindowsLoaded();
  188. }
  189. public void Logoff()
  190. {
  191. if (Current.EnableAccountModule)
  192. {
  193. AccountClient.Instance.Service.Logout(AccountClient.Instance.CurrentUser.AccountId);
  194. }
  195. _views.Logoff();
  196. }
  197. }
  198. }