问题
Afternoon people!
I'm trying to implement a list of counties on my Compact Framework (Mobile) application.
I can do this easily in the full .Net framework with CultureInfo.GetCultures(..etc). However, the CF seems to be missing this feature?
Is there any way I can return a list of countries (and regions if possible) that I can populate into a ComboBox?
The OS has a list of countries, so there must be a way to do it?
I look forward to hearing back!
回答1:
How about this?
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 List<CultureInfo> m_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<CultureInfo>();
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);
}
}
Usage looks like this:
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));
}
}
And output looks like this:
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)
回答2:
Unfortunatly CultureInfo.GetCultures method is not supported by .NET Compact Framework but you can iterate through available cultures on the device by checking on CultureType Enumeration. Here'slink a link to MSDN with some explanation and examples
来源:https://stackoverflow.com/questions/435951/compact-framework-retrieve-a-list-of-countries-and-regions