You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.1 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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
{
/// <summary>
/// 全局类
/// </summary>
public static class GlobalClass
{
static bool? inDesignMode = null;
/// <summary>
/// 判断是设计器还是程序运行
/// </summary>
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);
}
}
/// <summary>
/// 语言改变通知事件
/// </summary>
public static EventHandler<EventArgs> LanguageChangeEvent;
static Resource StringResource;
static GlobalClass()
{
StringResource = new Resource();
}
/// <summary>
/// 获取资源内容
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetString(string key)
{
return StringResource.GetString(key);
}
/// <summary>
/// 改变语言
/// </summary>
/// <param name="language">CultureInfo列表(http://www.csharpwin.com/csharpspace/8948r7277.shtml)</param>
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");
}
}
}