简体   繁体   中英

Where should I change/set culture info settings in my WPF app and why? (Must works on all .NET frameworks 4.0 and newer)

I'm trying to set up an a culture info for my WPF application and I've found several examples, like:

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US");

And something like this:

 protected override void OnStartup(StartupEventArgs e)

            {

                  Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); ;

                  Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US"); ;



                  FrameworkElement.LanguageProperty.OverrideMetadata(

                    typeof(FrameworkElement),

                    new FrameworkPropertyMetadata(

                          XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));



                  base.OnStartup(e);

           }

What is right way to achieve this? And where should I set it? I think setting it everytime windows is opened/loaded is not good idea?

And where should I set it?

Just open the auto-generated App.xaml.cs file and override the OnStartup method:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); ;
        Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US"); ;

        FrameworkElement.LanguageProperty.OverrideMetadata(
          typeof(FrameworkElement),
          new FrameworkPropertyMetadata(
                XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
        base.OnStartup(e);
    }
}

This method is called once when the application starts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM