C# 在Metro中,如何获得所有国家的列表?
在Metro中,C# 在Metro中,如何获得所有国家的列表?,c#,windows-8,microsoft-metro,C#,Windows 8,Microsoft Metro,在Metro中,CultureInfo.GetCultures方法不再存在。如果要检索包含所有可用国家/地区的字符串的完整列表,有哪些备选方案可用?请尝试以下方法: public class LocalesRetrievalException: Exception { public LocalesRetrievalException(string message): base(message) { } } #region
CultureInfo.GetCultures
方法不再存在。如果要检索包含所有可用国家/地区的字符串的完整列表,有哪些备选方案可用?请尝试以下方法:
public class LocalesRetrievalException: Exception
{
public LocalesRetrievalException(string message): base(message)
{
}
}
#region Windows API
private delegate bool EnumLocalesProcExDelegate(
[MarshalAs(UnmanagedType.LPWStr)]String lpLocaleString,
LocaleType dwFlags, int lParam);
[DllImport(@"kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool EnumSystemLocalesEx(EnumLocalesProcExDelegate pEnumProcEx,
LocaleType dwFlags, int lParam, IntPtr lpReserved);
private enum LocaleType : uint
{
LocaleAll = 0x00000000, // Enumerate all named based locales
LocaleWindows = 0x00000001, // Shipped locales and/or replacements for them
LocaleSupplemental = 0x00000002, // Supplemental locales only
LocaleAlternateSorts = 0x00000004, // Alternate sort locales
LocaleNeutralData = 0x00000010, // Locales that are "neutral" (language only, region data is default)
LocaleSpecificData = 0x00000020, // Locales that contain language and region data
}
#endregion
public enum CultureTypes : uint
{
SpecificCultures = LocaleType.LocaleSpecificData,
NeutralCultures = LocaleType.LocaleNeutralData,
AllCultures = LocaleType.LocaleWindows
}
public static IReadOnlyCollection<CultureInfo> GetCultures(
CultureTypes cultureTypes)
{
List<CultureInfo> cultures = new List<CultureInfo>();
EnumLocalesProcExDelegate enumCallback = (locale, flags, lParam) =>
{
try
{
cultures.Add(new CultureInfo(locale));
}
catch (CultureNotFoundException)
{
// This culture is not supported by .NET (not happened so far)
// Must be ignored.
}
return true;
};
if (EnumSystemLocalesEx(enumCallback, (LocaleType)cultureTypes, 0,
(IntPtr)0) == false)
{
int errorCode = Marshal.GetLastWin32Error();
throw new LocalesRetrievalException("Win32 error " + errorCode +
" while trying to get the Windows locales");
}
// Add the two neutral cultures that Windows misses
// (CultureInfo.GetCultures adds them also):
if (cultureTypes == CultureTypes.NeutralCultures ||
cultureTypes == CultureTypes.AllCultures)
{
cultures.Add(new CultureInfo("zh-CHS"));
cultures.Add(new CultureInfo("zh-CHT"));
}
return new ReadOnlyCollection<CultureInfo>(cultures);
}
public类LocalesRetrievalException:异常
{
公共LocalesRetrievalException(字符串消息):基本(消息)
{
}
}
#区域Windows API
私有委托bool EnumLocalesProcExDelegate(
[Marshallas(UnmanagedType.LPWStr)]字符串lpLocaleString,
LocaleType-dwFlags,int-lParam);
[DllImport(@“kernel32.dll”,SetLastError=true,CharSet=CharSet.Unicode)]
私有静态外部布尔EnumSystemLocalesEx(EnumLocalesProcExDelegate pEnumProcEx,
LocaleType dwFlags、intlparam、IntPtr lpReserved);
私有枚举LocaleType:uint
{
LocaleAll=0x00000000,//枚举所有基于命名的区域设置
LocaleWindows=0x00000001,//已装运区域设置和/或它们的替换项
LocaleSupplemental=0x00000002,//仅限补充地区
LocaleAlternateSorts=0x00000004,//备用排序区域设置
LocaleNeutralData=0x00000010,//为“中性”的区域设置(仅限语言,默认为区域数据)
LocalSpecificData=0x00000020,//包含语言和区域数据的区域设置
}
#端区
公共枚举文化类型:uint
{
SpecificCultures=LocaleType.localspecificData,
NeutralCultures=LocaleType.LocaleNeutralData,
AllCultures=LocaleType.LocaleWindows
}
公共静态IReadOnlyCollection GetCultures(
CultureTypes CultureTypes)
{
列表文化=新列表();
EnumLocalesProcExDelegate enumCallback=(区域设置、标志、LPRAM)=>
{
尝试
{
添加(新的CultureInfo(locale));
}
捕获(CultureNotFoundException)
{
//此区域性不受.NET支持(目前尚未出现)
//必须忽略这一点。
}
返回true;
};
如果(EnumSystemLocalesEx(enumCallback,(LocaleType)CultureType,0,
(IntPtr)0)=假)
{
int errorCode=Marshal.GetLastWin32Error();
抛出新的LocalesRetrievalException(“Win32错误”+errorCode+
“尝试获取Windows区域设置时”);
}
//添加Windows忽略的两个中性区域性
//(CultureInfo.GetCultures也会添加它们):
如果(cultureTypes==cultureTypes.NeutralCultures | |
cultureTypes==cultureTypes.AllCultures)
{
添加(新文化信息(“zh CHS”);
添加(新文化信息(“zh CHT”);
}
返回新的只读集合(区域性);
}
参考: