C# 如何在 WPF 应用程序中全局设置文化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1265773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:25:34  来源:igfitidea点击:

How can i globally set the Culture in a WPF Application?

c#.netwpfxamllocalization

提问by Botz3000

I would like to set the Culture of a WPF application to a specific one based on user preferences.

我想根据用户偏好将 WPF 应用程序的文化设置为特定的文化。

I can do this for the current thread via Thread.CurrentThread.Current(UI)Culture, but is there any way to do this globally for the application (so it affects all threads by default)?

我可以通过 为当前线程执行此操作Thread.CurrentThread.Current(UI)Culture,但是有什么方法可以为应用程序全局执行此操作(因此默认情况下它会影响所有线程)?

采纳答案by Wael Dalloul

there is no way to set it for all threads in the application, however if you are creating the threads in your application you can set the culture for everyone by yourself as you mentioned above

没有办法为应用程序中的所有线程设置它,但是如果您在应用程序中创建线程,您可以自己为每个人设置文化,如上所述

To set the Culture to the Main Application use the following snippet code:

要将文化设置为主应用程序,请使用以下代码片段:

Dim newCulture As CultureInfo = new CultureInfo("fr-FR")
CurrentThread.CurrentCulture = newCulture

回答by Scott Whitlock

Try putting

尝试放置

<UICulture>en-US</UICulture>

... in your csproj file.

...在您的 csproj 文件中。

回答by Darren

Or you could try this:

或者你可以试试这个:

 FrameworkElement.LanguageProperty.OverrideMetadata(GetType(FrameworkElement), New FrameworkPropertyMetadata(Markup.XmlLanguage.GetLanguage(Globalization.CultureInfo.CurrentCulture.IetfLanguageTag)))

回答by Matze

Or try building an appropriate Attached Property like this

或者尝试像这样构建一个合适的附加属性

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);
    }


}

回答by Gilad

ok so this is what I use in order to make sure all of my app is in a en-US culture.

好的,这就是我使用的方法,以确保我的所有应用程序都处于美国文化中。

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));

in order to make a single thread in a culture you can make

为了在文化中建立一个单一的线程,你可以

Thread.CurrentThread.CurrentCulture = new CultureInfo("EN-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("EN-US");