using System; using System.Globalization; using System.Linq; using System.Threading; using System.Windows; using System.Windows.Forms; using System.Windows.Media.Imaging; using Aitex.Core.Account; using Aitex.Core.RT.SCCore; using Aitex.Triton160.Common; using Aitex.Triton160.UI.Config; using Aitex.Triton160.UI.ViewModel; using Aitex.Triton160.UI.Views; using Aitex.Triton160.UI.Wcf; using Aitex.Core.UI.View.Frame; using Aitex.Core.Util; using Aitex.Core.RT.Log; using Application = System.Windows.Application; using MessageBox = System.Windows.Forms.MessageBox; namespace Aitex.Triton160.UI { class Triton160UiSystem : Singleton { public string CurrentUserName { get; set; } public Action CultureChanged; public string Culture { get; private set; } public WcfClient WCF { get { return WcfClient.Instance; } } public UiEvent EV { get { return _event; } } private UiEvent _event; private ViewManager _views; public bool Initialize() { try { _event = new UiEvent(); SystemConfigManager.Instance.Initialize(); Singleton.Instance.Initialize(); WcfClient.Instance.Initialize(); object language; int i = 0; do { language = WCF.Query.GetConfig(SCName.System_Language); i++; if (i == 100) break; Thread.Sleep(500); } while (language == null); _views = new ViewManager() { SystemName = "Triton160", SystemLogo = new BitmapImage(new Uri("pack://application:,,,/Resources/Triton160.ico", UriKind.Absolute)), UILayoutFile = SystemConfigManager.Instance.GetUiLayoutXmlFile() , ViewAssembly = "Triton160UI", }; SetCulture((language != null && (int)(language) == 2) ? "zh-CN" : "en-US"); //try { _views.ShowMainWindow(false); _views.OnMainWindowLoaded += views_OnMainWindowLoaded; //登录对话框 MainLogin mainLogin = new MainLogin(); if (mainLogin.ShowDialog() == true) { Account account = Triton160UiSystem.Instance.WCF.Account.GetAccountInfo(Triton160UiSystem.Instance.CurrentUserName).AccountInfo; _views.SetViewPermission(account); _views.MainWindow.Show(); } } } catch (Exception ex) { //System.Windows.Application.Current.Resources["All_Vehicles"] MessageBox.Show("系统初始化失败。\r\n" + ex.Message, "Triton160 UI系统初始化", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } return true; } void views_OnMainWindowLoaded() { _event.Start(); } public void Logoff() { try { WCF.Account.Logout(CurrentUserName); _views.Logoff(); } catch (Exception e) { LOG.Write(e); } } public void Terminate() { try { _event.Stop(); WcfClient.Instance.Terminate(); Singleton.Instance.Terminate(); } catch (Exception ex) { LOG.Write(ex); } } public void SetCulture(string culture) { SelectCulture(culture); _views.SetCulture(culture); Culture = culture; if (CultureChanged != null) { CultureChanged(culture); } } void SelectCulture(string culture) { //string culture = language == 2 ? "zh-CN" : "en-US"; //Copy all MergedDictionarys into a auxiliar list. var dictionaryList = Application.Current.Resources.MergedDictionaries.ToList(); //Search for the specified culture. string requestedCulture = string.Format(@"Resources/StringResources.{0}.xaml", culture); var resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString == requestedCulture); if (resourceDictionary == null) { //If not found, select our default language. requestedCulture = "StringResources.xaml"; resourceDictionary = dictionaryList. FirstOrDefault(d => d.Source.OriginalString == requestedCulture); } //If we have the requested resource, remove it from the list and place at the end. //Then this language will be our string table to use. if (resourceDictionary != null) { Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary); Application.Current.Resources.MergedDictionaries.Add(resourceDictionary); } //Inform the threads of the new culture. Thread.CurrentThread.CurrentCulture = new CultureInfo(culture); Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture); } } }