c# - Compact Framework - 检索国家和地区列表

标签 c# .net compact-framework cultureinfo

下午好!

我正在尝试在我的 Compact Framework(移动)应用程序上实现一个县列表。

我可以在完整的 .Net 框架中使用 CultureInfo.GetCultures(..etc) 轻松完成此操作。不过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 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);
    }
}

用法看起来像这样:

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)

关于c# - Compact Framework - 检索国家和地区列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/435951/

相关文章:

c# - 无法让 ApplyCurrentValues(Entity) 在 Entity Framework 5 中工作

c# - xamarin.ios mp3 在屏幕关闭时停止播放

.net - Visual Studio 文档 XML 注释标签中神秘的 "usage"标签

c# .Net CF Form.Invoke 引发 ArgumentException

c# - 从不同的类访问控件

c# - 从文件 c# 添加到注册表项

.net - DataGridView 绑定(bind)到DataTable。获取枚举的组合框

c# - 在 .Net 中使用 Spark 写入增量表

紧凑框架上的 C# Double.TryParse 等效项

c# - 自适应Windows移动应用程序