Windows 10 Windows Universal中CultureInfo之间的不一致
我需要从UWP应用程序生成一个.CSV文件,所以我使用TextInfo.listselector 我发现系统设置和代码返回的值不一致 使用区域设置:Windows 10 Windows Universal中CultureInfo之间的不一致,windows-10,windows-10-universal,Windows 10,Windows 10 Universal,我需要从UWP应用程序生成一个.CSV文件,所以我使用TextInfo.listselector 我发现系统设置和代码返回的值不一致 使用区域设置: TextInfo textInfo = System.Globalization.CultureInfo.CurrentCulture.TextInfo; System.Diagnostics.Debug.WriteLine(textInfo.CultureName); System.Diagnostics.Debug.WriteLine(text
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);
使用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显示语言:英语(美国)
- 区域格式:葡萄牙语(巴西)
- 所在地区:美国
Windows.Globalization.ApplicationLanguages.primarylanguagesoverride=“pt br”
(通常在App.xaml.cs中的OnLaunched事件中)在UWP应用程序中,您将获得您所期望的,但这并非来自您在区域设置中自定义的设置。这就是不同设备系列的通用应用程序的工作原理。请查看MSDN文档以了解它的实际功能
仅供参考,UWP使用的.net核心API中的CurrentCulture可在上找到。以下是它的实施方式:
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;
}
}
公共静态文化信息CurrentCulture
{
得到
{
Contract.sure(Contract.Result()!=null);
#如果!功能\u CORECLR
返回Thread.CurrentThread.CurrentCulture;
#否则
//对于CoreCLR,Thread.m_CurrentCulture和
//Thread.m_CurrentUICulture是线程静态的,以避免
//CultureInfo对象跨AppDomain边界泄漏
//这些字段是线程静态的这一事实引入了开销
//在访问它们时(通过Thread.CurrentCulture)
//在本例中,访问Thread.CurrentThread
//在这种情况下,我们可以避免Thread.CurrentThread的开销
//因为这些字段是线程静态的,所以它们不是线程静态的
//需要访问线程实例。
#如果功能_APPX
if(AppDomain.IsAppXModel()){
CultureInfo culture=GetCultureInfo-ForUserPreferredLanguageInAppx();
if(区域性!=null)
回归文化;
}
#恩迪夫
返回Thread.m_CurrentCulture??
s_默认ThreadCurrentCulture??
s_userDefaultCulture??
用户文化;
#恩迪夫
}
设置
{
#如果功能_APPX
如果(值==null){
抛出新的ArgumentNullException(“值”);
}
if(AppDomain.IsAppXModel()){
if(SetCultureInfoForUserPreferredLanguageInAppX(值)){
//成功设置区域性,否则返回到旧路径
返回;
}
}
#恩迪夫
Thread.CurrentThread.CurrentCulture=值;
}
}
这对我来说很有效
我认为这是因为更改设置后没有重新打开visual studio
更改设置后,它不会通知并强制已打开的visual studio(或其他应用程序)更改其环境值。您必须关闭并重新打开visual studio,才能让visual studio以新设置开始
[更新]
我让它工作,因为我以为你只是想改变ListSeparator设置,但没有改变区域格式@user5596450的方向正确。所以你的问题的答案是否定的。无法获取在“区域设置”中指定的自定义区域格式
实际上,自定义设置并不适用于所有设备系列。我相信目前的结果是有道理的。您所期望的应该是UWP的桌面扩展,而不是.net核心API。您可以随时向wpdev.uservoice.com提出请求,Microsoft正在那里听取开发人员的反馈
作为替代,.Net core API将检查UWP应用程序的首选语言,并向您获取相应的设置。例如,如果调用Windows.Globalization.ApplicationLanguages.primarylanguagesoverride=“pt br”
(通常在App.xaml.cs中的OnLaunched事件中)在UWP应用程序中,您将获得您所期望的,但这并非来自您在区域中自定义的设置