【问题标题】:Inconsistencies between CultureInfo in Windows UniversalWindows Universal 中 CultureInfo 之间的不一致
【发布时间】:2015-11-23 12:43:02
【问题描述】:

我需要从 UWP 应用生成 .CSV 文件,因此我使用 TextInfo.ListSeparator。

我发现系统设置与代码返回的值不一致。

使用区域设置

使用 TextInfo 类

TextInfo textInfo = System.Globalization.CultureInfo.CurrentCulture.TextInfo;
System.Diagnostics.Debug.WriteLine(textInfo.CultureName);
System.Diagnostics.Debug.WriteLine(textInfo.IsReadOnly);
System.Diagnostics.Debug.WriteLine(textInfo.ListSeparator);
System.Diagnostics.Debug.WriteLine(textInfo.IsRightToLeft);

我的系统已配置:

  • Windows 显示语言:英语(美国)
  • 区域格式:葡萄牙语(巴西)
  • 地区首页位置:美国

已编辑 正如一些答案中所建议的,我重新启动了我的电脑。然后编写了一个 UWP 和一个使用相同代码的 Windows 窗体应用程序。为了比较,我运行了一个 Windows PowerShell。如下图所示,这些值仍然不同。

PowerShell 和 Windows 窗体返回预期结果,但 UWP 失败。

【问题讨论】:

  • 我看到 .NETCore 运行时做对了,它直接调用 GetLocaleInfoEx() 来获取 Windows 设置。所以它一定是你的机器配置。

标签: windows-10 windows-10-universal


【解决方案1】:

这对我有用。

我认为这是因为您更改设置后没有重新打开您的视觉工作室。

更改设置后,它不会通知并强制已打开的 Visual Studio(或其他应用程序)更改其环境值。您必须关闭并重新打开您的 Visual Studio 才能让 Visual Studio 以新设置启动。

[更新]

我让它工作了,因为我认为您只想更改 ListSeparator 设置,但没有更改 Region 格式。 @user5596450 方向正确。因此,您的问题的答案是。您无法获取您在区域设置中指定的自定义区域格式。

实际上,自定义设置并非适用于所有设备系列。我相信目前的结果是有道理的。您所期望的应该由诸如 UWP 的桌面扩展之类的东西提供,而不是 .net 核心 API。您可以随时向 wpdev.uservoice.com 提出请求,Microsoft 正在那里听取开发人员的反馈。

作为替代方案,.Net 核心 API 将检查 UWP 应用程序的 PreferredLanguage 并为您获取相应的设置。例如,如果您在 UWP 应用中调用 Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "pt-br";(通常在 App.xaml.cs 中的 OnLaunched 事件中),您将得到您所期望的但这不是来自您在区域设置中自定义的设置.这就是它应该如何适用于不同设备系列的通用应用程序。请查看ApplicationLanguages.PrimaryLanguageOverride MSDN 文档以了解它的实际作用。

仅供参考,UWP 使用的 .net 核心 API 中的 CurrentCulture 可以在 github 上找到。以下是它的实现方式:

public static CultureInfo CurrentCulture
{
    get
    {
        Contract.Ensures(Contract.Result<CultureInfo>() != null);

#if !FEATURE_CORECLR
        return Thread.CurrentThread.CurrentCulture;
#else
                // In the case of CoreCLR, Thread.m_CurrentCulture and
                // Thread.m_CurrentUICulture are thread static so as not to let
                // CultureInfo objects leak across AppDomain boundaries. The
                // fact that these fields are thread static introduces overhead
                // in accessing them (through Thread.CurrentCulture). There is
                // also overhead in accessing Thread.CurrentThread. In this
                // case, we can avoid the overhead of Thread.CurrentThread
                // because these fields are thread static, and so do not
                // require a Thread instance to be accessed.
#if FEATURE_APPX
                if(AppDomain.IsAppXModel()) {
                    CultureInfo culture = GetCultureInfoForUserPreferredLanguageInAppX();
                    if (culture != null)
                        return culture;
                }
#endif
                return Thread.m_CurrentCulture ??
                    s_DefaultThreadCurrentCulture ??
                    s_userDefaultCulture ??
                    UserDefaultCulture;
#endif
    }

    set
    {
#if FEATURE_APPX
                    if (value == null) {
                        throw new ArgumentNullException("value");
                    }                    

                    if (AppDomain.IsAppXModel()) {
                        if (SetCultureInfoForUserPreferredLanguageInAppX(value)) {
                            // successfully set the culture, otherwise fallback to legacy path
                            return; 
                        }
                    }
#endif
        Thread.CurrentThread.CurrentCulture = value;
    }
}

【讨论】:

  • 不幸的是,即使重新启动 PC,问题仍然存在。请在我的问题中查看我编辑的评论。谢谢
  • 检查我的更新。 @user5596450 在正确的轨道上。您的问题的简单答案是否定的。但看看你是否可以接受另一种方式。
【解决方案2】:

上面的答案可能是正确的。然而,更可能的原因是 Win32 或系统 API 使用基于特定区域格式设置(在本例中为 pt-BR)的区域设置数据,即“;”。 UWP 使用 WinRT API,它使用基于 Windows 显示语言(在本例中为 en-US)的区域设置数据,即“,”。

【讨论】:

  • 我做了一些测试来验证你的建议,这是有道理的。请在我的问题中查看我编辑的评论。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多