C#学习教程:替换.NETWindows应用商店应用程序中的CultureInfo.GetCultures我们如何阅读所有可用的文化?我知道语言列表和主要应用语言。我可以通过这种方式读取应用程序可用的所有语言。但我需要阅读系统上所有可用的文化(语言)。过去使用CultureInfo.GetCultures很容易。詹姆斯的评论为我指明了正确的方向。这是我开发的代码。我用单元测试检查了代码,以确保返回的中性、特定和所有文化与CultureInfo.GetCultures返回的相同(它们确实是:-))。以上为C#学习教程:替换.NETWindows应用商店应用中CultureInfo.GetCultures分享的所有内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注——publicclassLocalesRetrievalException:Exception{publicLocalesRetrievalException(stringmessage):base(message){}}CultureHelper{#regionWindowsAPI私有委托boolEnumLocalesProcExDelegate([MarshalAs(UnmanagedType.LPWStr)]StringlpLocaleString,LocaleTypedwFlags,int"lParam";[DllImport(kernel32.dll",SetLastError=true,CharSet=CharSet.Unicode)]privatestaticexternboolEnumSystemLocalesEx(EnumLocalesProcExDelegatepEnumProcEx,LocaleTypedwFlags,intlParam,IntPtrlpReserved);privateenumLocaleType:uint{Locale0d0ate0all0//Enum0All0=Enum0basedlocalesLocaleWindows=0x00000001,//Shippedlocalesand/orreplacementsforthemLocaleSupplemental=0x00000002,//仅补充区域设置LocaleAlternateSorts=0x00000004,//替代排序区域设置LocaleNeutralData=0x0x00000010,//“中性”区域设置(仅语言,默认区域数据)LocaleSpecificData=0x00000020,//包含语言和区域数据的区域设置}#endregionpublicenumCultureTypes:uint{SpecificCultures=LocaleType.LocaleSpecificData,NeutralCultures=LocaleType.LocaleNeutralData,AllCultures=LocaleType.LocaleWindows}publicstaticIReadOnlyCollectionGetCultures(CultureTypescultureTypes){列表文化=newList();EnumLocalesProcExDelegateenumCallback=(locale,flags,lParam)=>{try{cultures.Add(newCultureInfo(locale));}catch(CultureNotFoundException){//.NET不支持此区域性(目前尚未发生)//必须忽略。}返回真;};如果(EnumSystemLocalesEx(enumCallback,(LocaleType)cultureTypes,0,(IntPtr)0)==false){interrorCode=Marshal.GetLastWin32Error();thrownewLocalesRetrievalException("Win32error"+errorCode+"whiletryingtogettheWindowslocales");}//添加tWindows遗漏的两种中性文化//(CultureInfo.GetCultures也添加了它们):;cultures.Add(newCultureInfo("zh-CHT"));}返回新的ReadOnlyCollection(文化);}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。明源:
