如何在WPF应用程序中全局设置文化?
我想根据用户首选项将WPF应用程序的文化设置为特定的文档。
我可以通过Thread.CurrentThread.Current(UI)Culture
为当前线程执行此操作,但有没有办法为应用程序全局执行此操作(因此默认情况下它会影响所有线程)?
没有办法为应用程序中的所有线程设置它,但是如果您在应用程序中创建线程,您可以自己为每个人设置文化,如上所述
要将Culture设置为Main Application,请使用以下代码段:
Dim newCulture As CultureInfo = new CultureInfo("fr-FR") CurrentThread.CurrentCulture = newCulture
试试吧
en-US
…在你的csproj文件中。
好吧,这就是我使用的,以确保我的所有应用程序都在en-US文化中。
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US"); CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US"); XmlLanaguage lang = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag); FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(lang)); FrameworkContentElement.LanguageProperty.OverrideMetadata(typeof(System.Windows.Documents.TextElement), new FrameworkPropertyMetadata(lang));
为了在文化中制作单个post,你可以制作
Thread.CurrentThread.CurrentCulture = new CultureInfo("EN-US"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("EN-US");
或者你可以试试这个:
FrameworkElement.LanguageProperty.OverrideMetadata(GetType(FrameworkElement), New FrameworkPropertyMetadata(Markup.XmlLanguage.GetLanguage(Globalization.CultureInfo.CurrentCulture.IetfLanguageTag)))
或者尝试构建这样的适当附加属性
public class CultureHelper : DependencyObject { public string Culture { get { return (string)GetValue(CultureProperty); } set { SetValue(CultureProperty, value); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty CultureProperty = DependencyProperty.RegisterAttached("Culture", typeof(string), typeof(CultureHelper), new FrameworkPropertyMetadata("en", CultureChanged)); private static void CultureChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { //For testing purposes in designer only if (DesignerProperties.GetIsInDesignMode(d)) { Thread.CurrentThread.CurrentUICulture = new CultureInfo((string)e.NewValue); } } public static void SetCulture(DependencyObject element, string value) { element.SetValue(CultureProperty, value); } public static string GetCulture(DependencyObject element) { return (string)element.GetValue(CultureProperty); } }