3

Metro では、このCultureInfo.GetCulturesメソッドはもう存在しません。利用可能なすべての国を含む文字列の完全なリストを取得する場合、どのような選択肢がありますか?

4

1 に答える 1

4

これを試してください:

    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);
    }

参照: .NET Windows ストア アプリでの CultureInfo.GetCultures の置き換え

于 2012-09-29T14:28:24.390 に答える