using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Globalization; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; namespace MaterialTraceabilityUI.Resources { /// /// 全局类 /// public static class GlobalClass { static bool? inDesignMode = null; /// /// 判断是设计器还是程序运行 /// public static bool InDesignMode { get { if (inDesignMode == null) { #if SILVERLIGHT inDesignMode = DesignerProperties.IsInDesignTool; #else var prop = DesignerProperties.IsInDesignModeProperty; inDesignMode = (bool)DependencyPropertyDescriptor.FromProperty(prop, typeof(FrameworkElement)).Metadata.DefaultValue; if (!inDesignMode.GetValueOrDefault(false) && Process.GetCurrentProcess().ProcessName.StartsWith("devenv", StringComparison.Ordinal)) inDesignMode = true; #endif } return inDesignMode.GetValueOrDefault(false); } } /// /// 语言改变通知事件 /// public static EventHandler LanguageChangeEvent; static Resource StringResource; static GlobalClass() { StringResource = new Resource(); } /// /// 获取资源内容 /// /// /// public static string GetString(string key) { return StringResource.GetString(key); } /// /// 改变语言 /// /// CultureInfo列表(http://www.csharpwin.com/csharpspace/8948r7277.shtml) public static void ChangeLanguage(string language) { CultureInfo culture = new CultureInfo(language); Thread.CurrentThread.CurrentCulture = culture; Thread.CurrentThread.CurrentUICulture = culture; StringResource.CurrentCulture = culture; // 如果是WPF应用,需要更新应用级别的文化 if (Application.Current != null) { Application.Current.MainWindow?.UpdateLayout(); // 重新加载资源字典(如果有语言资源文件) //UpdateResourceDictionary(cultureName); } if (LanguageChangeEvent != null) { LanguageChangeEvent(null, null); } } // 检查当前是否为中文 public static bool IsChinese() { return Thread.CurrentThread.CurrentUICulture.Name.StartsWith("zh"); } // 检查当前是否为英文 public static bool IsEnglish() { return Thread.CurrentThread.CurrentUICulture.Name.StartsWith("en"); } } }