如何在WPF中更改应用程序文化?

4

这是我的代码。

double value = double.Parse(Utility.GetParamValueOrDefault(omRecord.paramList[i].value, "0"),CultureInfo.CurrentCulture);

这是我遇到的错误: 格式异常:输入字符串格式不正确 我看了一些StackOverFlow上的帖子,说我需要在WPF应用程序的main()函数中添加以下代码。
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(
XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

我仍然会得到相同的错误,并且我的 CurrentCulture 仍然不是 en-US。

Thread.CurrentThread 只针对一个线程 - 你确定你展示的代码在该线程中运行,而不是另一个线程中吗? - stijn
它在另一个线程中运行,我该如何将其设置为整个应用程序? - Gilad
你把解析逻辑放在哪里?像BackgroundWorker一样在新线程中吗?而设置文化应该在应用程序开始时完成。 - Yuliam Chandra
在代码的每一行中,我都解析了一些东西,我需要这样做吗? - Gilad
@Gilad - 你是否在指定区域文化方面遇到了问题?对生产代码运行代码分析可能是个好主意,这样你会得到所有缺少它的解析/格式化函数的CA1304:指定 CultureInfo警告... - Alexei Levenkov
显示剩余4条评论
2个回答

19

试试这个

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

6
在应用程序开始时,您可以通过以下方式设置CultureInfo:
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");

如果您想在运行时确定CultureInfo,请尝试以下方法:

System.Globalization.CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;

对于英语,有以下表格 :-)

en          en-US       English
en-029      en-029      English (Caribbean)
en-AU       en-AU       English (Australia)
en-BZ       en-BZ       English (Belize)
en-CA       en-CA       English (Canada)
en-GB       en-GB       English (United Kingdom)
en-IE       en-IE       English (Ireland)
en-JM       en-JM       English (Jamaica)
en-NZ       en-NZ       English (New Zealand)
en-PH       en-PH       English (Republic of the Philippines)
en-TT       en-TT       English (Trinidad and Tobago)
en-US       en-US       English (United States)
en-ZA       en-ZA       English (South Africa)
en-ZW       en-ZW       English (Zimbabwe)

这里是所有语言的链接 https://wiki.openstreetmap.org/wiki/Nominatim/Country_Codes


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接