下午的人!
我正在尝试在我的Compact Framework(移动)应用程序上实现一个县列表.
我可以使用CultureInfo.GetCultures(.. etc)在完整的.Net框架中轻松完成此操作.但是,CF似乎缺少这个功能?
有什么方法可以返回我可以填充到ComboBox中的国家/地区列表(如果可能的话,还有地区)?
操作系统有一个国家列表,所以必须有办法吗?
我期待着回复!
这个怎么样?
using System; using System.Collections.Generic; using System.Globalization; using System.Runtime.InteropServices; namespace OpenNETCF.Globalization { public class CultureInfoHelper { private delegate int EnumLocalesHandler(string lpLocaleString); private static EnumLocalesHandler m_localesDelegate; private static Listm_cultures; private static int EnumLocalesProc(string locale) { try { m_cultures.Add(CultureInfo.GetCultureInfo( int.Parse(locale, NumberStyles.HexNumber))); } catch { // failed for this locale - ignore and continue } return 1; } public static CultureInfo[] GetCultures() { if (m_localesDelegate == null) { m_cultures = new List (); m_localesDelegate = new EnumLocalesHandler(EnumLocalesProc); IntPtr fnPtr = Marshal.GetFunctionPointerForDelegate( m_localesDelegate); int success = EnumSystemLocales(fnPtr, LCID_INSTALLED); } return m_cultures.ToArray(); } private const int LCID_INSTALLED = 0x01; private const int LCID_SUPPORTED = 0x02; [DllImport("coredll", SetLastError = true)] private static extern int EnumSystemLocales( IntPtr lpLocaleEnumProc, uint dwFlags); } }
用法如下:
using OpenNETCF.Globalization; .... static void Main() { foreach (CultureInfo ci in CultureInfoHelper.GetCultures()) { Debug.WriteLine(string.Format("0x{0:x2}({1}) : {2}", ci.LCID, ci.Name, ci.EnglishName)); } }
输出看起来像这样:
0x402(bg-BG) : Bulgarian (Bulgaria) 0x403(ca-ES) : Catalan (Catalan) 0x405(cs-CZ) : Czech (Czech Republic) 0x406(da-DK) : Danish (Denmark) 0x407(de-DE) : German (Germany) 0x408(el-GR) : Greek (Greece) 0x409(en-US) : English (United States) ... 0x400a(es-BO) : Spanish (Bolivia) 0x440a(es-SV) : Spanish (El Salvador) 0x480a(es-HN) : Spanish (Honduras) 0x4c0a(es-NI) : Spanish (Nicaragua) 0x500a(es-PR) : Spanish (Puerto Rico)