【问题标题】:WINRT TextBox Number Formatting not respecting Current CultureWINRT 文本框数字格式不尊重当前文化
【发布时间】:2012-08-09 11:07:07
【问题描述】:

我正在将一个浮点属性绑定到我的 TextBox 控件。如果我使用“en”作为我的语言首选项,那是正确的。 输入数字“1.123”被正确解析。

如果我将 mur 文化切换为“de”,则 TextBox(和 TextBlocks)中没有任何变化。仍然所有内容都用“.”格式化。

通过 WinRT 的“数字”键盘输入数字是使用“,”作为分隔符。但是在 Focus 丢失后,Training Digits 将不再是数字的一部分(并且绑定属性已更新。 所以“1,234”应该在内部解析为“1.234”。但 ist 转换为“1”。

app.xaml.cs 包含更多初始化,以确保将文化设置为正确的语言。允许的语言是 ("en" 和 "de" (*,proj file. 如果您闯入调试器,CultureInfo.CurrentCulture 会正确设置为“de”。

public App()
{
    Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride =     CultureInfo.CurrentCulture.Name;
    CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CurrentCulture;
    CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CurrentCulture;

【问题讨论】:

  • 你们如何改变文化?如果通过 Windows 更改用户的文化,您是否确保应用程序被终止然后重新启动? CultureInfo.CurrentCulture 表示文化仅在线程创建时设置。
  • 我正在通过 Windows 设置语言。 App.cs 包含用于确保分配正确语言的附加设置:

标签: wpf microsoft-metro windows-runtime cultureinfo


【解决方案1】:

Application 构造函数中设置PrimaryLanguageOverrideDefaultThreadCurrentCultureDefaultThreadCurrentUICulture 无效,因为它们仅适用于新线程。但是,您的主 UI 线程已经创建,并且在进入构造函数时设置了其文化。

要强制更改现有 UI 线程,您必须设置 Thread.CurrentCulture,如 CultureInfo.CurrentCulture 中所述;但是,WinRT 不支持 Thread 类。

这就引出了一个问题,为什么需要进行任何手动文化改变。应用应在启动时或从休眠状态恢复时自动选择用户的 Windows 文化。

【讨论】:

  • Thread.CurrentCulture 不幸的是,当我没有完全错时,WinRT 不支持... WPF !=WinRT。我认为从系统中正确设置了文化 - 但绑定数字的格式和解析不正确。
  • 关于无法在 WinRT 中的现有线程上设置区域性的要点。我相应地更新了答案。为什么系统文化不能与数据绑定“正常工作”仍然是个谜。
【解决方案2】:

要获得我们可以使用的 InvariantCulture 设置,

“ApplicationLanguages.PrimaryLanguageOverride = CultureInfo.InvariantCulture.TwoLetterISOLanguageName;”

这对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 2014-04-08
    • 2023-01-05
    • 1970-01-01
    相关资源
    最近更新 更多