Triton160UiSystem.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Windows;
  6. using System.Windows.Forms;
  7. using System.Windows.Media.Imaging;
  8. using Aitex.Core.Account;
  9. using Aitex.Core.RT.SCCore;
  10. using Aitex.Triton160.Common;
  11. using Aitex.Triton160.UI.Config;
  12. using Aitex.Triton160.UI.ViewModel;
  13. using Aitex.Triton160.UI.Views;
  14. using Aitex.Triton160.UI.Wcf;
  15. using Aitex.Core.UI.View.Frame;
  16. using Aitex.Core.Util;
  17. using Aitex.Core.RT.Log;
  18. using Application = System.Windows.Application;
  19. using MessageBox = System.Windows.Forms.MessageBox;
  20. namespace Aitex.Triton160.UI
  21. {
  22. class Triton160UiSystem : Singleton<Triton160UiSystem>
  23. {
  24. public string CurrentUserName
  25. {
  26. get; set;
  27. }
  28. public Action<string> CultureChanged;
  29. public string Culture
  30. {
  31. get; private set;
  32. }
  33. public WcfClient WCF { get { return WcfClient.Instance; } }
  34. public UiEvent EV { get { return _event; } }
  35. private UiEvent _event;
  36. private ViewManager _views;
  37. public bool Initialize()
  38. {
  39. try
  40. {
  41. _event = new UiEvent();
  42. SystemConfigManager.Instance.Initialize();
  43. Singleton<LogManager>.Instance.Initialize();
  44. WcfClient.Instance.Initialize();
  45. object language;
  46. int i = 0;
  47. do
  48. {
  49. language = WCF.Query.GetConfig(SCName.System_Language);
  50. i++;
  51. if (i == 100)
  52. break;
  53. Thread.Sleep(500);
  54. } while (language == null);
  55. _views = new ViewManager()
  56. {
  57. SystemName = "Triton160",
  58. SystemLogo = new BitmapImage(new Uri("pack://application:,,,/Resources/Triton160.ico", UriKind.Absolute)),
  59. UILayoutFile = SystemConfigManager.Instance.GetUiLayoutXmlFile() ,
  60. ViewAssembly = "Triton160UI",
  61. };
  62. SetCulture((language != null && (int)(language) == 2) ? "zh-CN" : "en-US");
  63. //try
  64. {
  65. _views.ShowMainWindow(false);
  66. _views.OnMainWindowLoaded += views_OnMainWindowLoaded;
  67. //登录对话框
  68. MainLogin mainLogin = new MainLogin();
  69. if (mainLogin.ShowDialog() == true)
  70. {
  71. Account account = Triton160UiSystem.Instance.WCF.Account.GetAccountInfo(Triton160UiSystem.Instance.CurrentUserName).AccountInfo;
  72. _views.SetViewPermission(account);
  73. _views.MainWindow.Show();
  74. }
  75. }
  76. }
  77. catch (Exception ex)
  78. {
  79. //System.Windows.Application.Current.Resources["All_Vehicles"]
  80. MessageBox.Show("系统初始化失败。\r\n" + ex.Message, "Triton160 UI系统初始化", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  81. return false;
  82. }
  83. return true;
  84. }
  85. void views_OnMainWindowLoaded()
  86. {
  87. _event.Start();
  88. }
  89. public void Logoff()
  90. {
  91. try
  92. {
  93. WCF.Account.Logout(CurrentUserName);
  94. _views.Logoff();
  95. }
  96. catch (Exception e)
  97. {
  98. LOG.Write(e);
  99. }
  100. }
  101. public void Terminate()
  102. {
  103. try
  104. {
  105. _event.Stop();
  106. WcfClient.Instance.Terminate();
  107. Singleton<LogManager>.Instance.Terminate();
  108. }
  109. catch (Exception ex)
  110. {
  111. LOG.Write(ex);
  112. }
  113. }
  114. public void SetCulture(string culture)
  115. {
  116. SelectCulture(culture);
  117. _views.SetCulture(culture);
  118. Culture = culture;
  119. if (CultureChanged != null)
  120. {
  121. CultureChanged(culture);
  122. }
  123. }
  124. void SelectCulture(string culture)
  125. {
  126. //string culture = language == 2 ? "zh-CN" : "en-US";
  127. //Copy all MergedDictionarys into a auxiliar list.
  128. var dictionaryList = Application.Current.Resources.MergedDictionaries.ToList();
  129. //Search for the specified culture.
  130. string requestedCulture = string.Format(@"Resources/StringResources.{0}.xaml", culture);
  131. var resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString == requestedCulture);
  132. if (resourceDictionary == null)
  133. {
  134. //If not found, select our default language.
  135. requestedCulture = "StringResources.xaml";
  136. resourceDictionary = dictionaryList.
  137. FirstOrDefault(d => d.Source.OriginalString == requestedCulture);
  138. }
  139. //If we have the requested resource, remove it from the list and place at the end.
  140. //Then this language will be our string table to use.
  141. if (resourceDictionary != null)
  142. {
  143. Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
  144. Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
  145. }
  146. //Inform the threads of the new culture.
  147. Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
  148. Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
  149. }
  150. }
  151. }