c# - .NET Windows 应用商店应用程序中 CultureInfo.GetCultures 的替代品

标签 c# windows-runtime microsoft-metro windows-store-apps

Windows 应用商店应用程序的 .NET API 中不存在 CultureInfo.GetCultures。我们如何才能读取所有可用的文化?

我知道语言列表和主要应用程序语言。我可以通过这种方式阅读该应用程序可用的所有语言。但我需要阅读系统上可用的所有文化(语言)。以前使用 CultureInfo.GetCultures 很容易。

最佳答案

James 的评论为我指明了正确的方向。这是我开发的代码。我使用单元测试检查了代码,以确保返回的中性、特定和所有文化与 CultureInfo.GetCultures 返回的文化相同(它们确实是 :-))。

public class LocalesRetrievalException : Exception
{
    public LocalesRetrievalException(string message)
        : base(message)
    {
    }
}

public static class CultureHelper
{
    #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);
    }
}

关于c# - .NET Windows 应用商店应用程序中 CultureInfo.GetCultures 的替代品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12485626/

相关文章:

c# - Process Stream类不支持写

c# - 从数据库中获取浮点值

c# - 等待输入只读属性消失

windows-8 - Windows RT 中的网络资源限制

c# - 使用 JSON 的 WCF RESTful POST,来自 Windows 应用商店应用

c# - 使用程序集名称和类类型动态创建类对象

windows-runtime - 字体系列在 Windows Phone 8.1 中无效

c# - 为 URI 关联注册应用程序 (Windows Phone 8.1 RT)

c# - 如何使用 C# 构建 PathGeometry?

c++ - 在 C++/CX 中解析 JSON ISO8601 日期