UiApplication.cs 10 KB

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