C# CultureInfo.GetCultures 返回空列表

发布于 2024-11-28 03:53:17 字数 1277 浏览 1 评论 0原文

我无法使用 GetCultures,据我所知它返回一个空白列表。

private void AddressChooser_Load(object sender, EventArgs e)
    {
        MessageBox.Show("Form load event successfully triggered") //Debug message - This appears at runtime
        foreach (string country in GetCountryList())
        {
            MessageBox.Show(country); //Debug message - This does not appear at runtime!!
            countryBox.Items.Clear();
            countryBox.Items.Add(country);
        }
    }

    public static List<string> GetCountryList()
    {
        MessageBox.Show("Function has been triggered successfully"); //Debug message - This appears at runtime
        List<string> cultureList = new List<string>();
        CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
        foreach (CultureInfo culture in cultures)
        {
            RegionInfo region = new RegionInfo(culture.LCID);
            if (!(cultureList.Contains(region.EnglishName)))
                cultureList.Add(region.EnglishName);
            MessageBox.Show(region.EnglishName); //Debug message - This does not appear at runtime!
        }
        return cultureList;
    }

我觉得奇怪的是,考虑到它只是一个复制和粘贴的片段,这不起作用。 请帮忙! 谢谢

I cannot use GetCultures, from what I can tell it returns a blank list.

private void AddressChooser_Load(object sender, EventArgs e)
    {
        MessageBox.Show("Form load event successfully triggered") //Debug message - This appears at runtime
        foreach (string country in GetCountryList())
        {
            MessageBox.Show(country); //Debug message - This does not appear at runtime!!
            countryBox.Items.Clear();
            countryBox.Items.Add(country);
        }
    }

    public static List<string> GetCountryList()
    {
        MessageBox.Show("Function has been triggered successfully"); //Debug message - This appears at runtime
        List<string> cultureList = new List<string>();
        CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
        foreach (CultureInfo culture in cultures)
        {
            RegionInfo region = new RegionInfo(culture.LCID);
            if (!(cultureList.Contains(region.EnglishName)))
                cultureList.Add(region.EnglishName);
            MessageBox.Show(region.EnglishName); //Debug message - This does not appear at runtime!
        }
        return cultureList;
    }

I find it strange that this doesn't work considering it is simply a copy&pasted snippet.
Please help!
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乜一 2024-12-05 03:53:17

您一定是在地板垫下的某个地方清扫了异常。

您的代码失败,因为 CultureTypes.AllCultures & ~CultureTypes.NeutralCultures 不起作用。您的列表包含中性文化和 new RegionInfo() 抛出。

代码片段:

var c1 = CultureTypes.AllCultures & ~CultureTypes.NeutralCultures;
Console.WriteLine(c1);

产生 SpecificCultures, InstalledWin32Cultures 并且我认为这些 Win32 区域性包含中性区域性。

简单的解决方案是 CultureInfo.GetCultures(CultureTypes.SpecificCultures);

但要解决的主要问题是异常处理和调试技术。

You must be sweeping an Exception under the floormat somewhere.

Your code fails because CultureTypes.AllCultures & ~CultureTypes.NeutralCultures doesn't work. Your list contains neutral cultures and new RegionInfo() throws.

The snippet:

var c1 = CultureTypes.AllCultures & ~CultureTypes.NeutralCultures;
Console.WriteLine(c1);

produces SpecificCultures, InstalledWin32Cultures and I suppose those Win32 cultures contain neutral ones.

The simple solution would be CultureInfo.GetCultures(CultureTypes.SpecificCultures);

But the main thing to fix would be your Exception handling and debugging techniques.

紙鸢 2024-12-05 03:53:17

首先,最好是“调试”>“异常”并将 CLR 运行时异常设置为“抛出”。其次,我认为这段代码可能就是您正在寻找的:

        static void Main()
        {
            List<string> cultureList = new List<string>();
            CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
            foreach (CultureInfo culture in cultures)
            {   
                try
                {
                    RegionInfo region = new RegionInfo(culture.Name);
                    if (!(cultureList.Contains(region.EnglishName)))
                        cultureList.Add(region.EnglishName);
                    Console.WriteLine(region.EnglishName); 
                }
                catch (ArgumentException e)
                {
                    Console.WriteLine(String.Format("For{0} a specific culture name is required.", culture.Name));
                }                  
            }
        }

Firstly it is a good idea to Debug>>Exceptions and set the CLR Runtime Execptions to Thrown . Secondly I think this code can be what you are looking for:

        static void Main()
        {
            List<string> cultureList = new List<string>();
            CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
            foreach (CultureInfo culture in cultures)
            {   
                try
                {
                    RegionInfo region = new RegionInfo(culture.Name);
                    if (!(cultureList.Contains(region.EnglishName)))
                        cultureList.Add(region.EnglishName);
                    Console.WriteLine(region.EnglishName); 
                }
                catch (ArgumentException e)
                {
                    Console.WriteLine(String.Format("For{0} a specific culture name is required.", culture.Name));
                }                  
            }
        }
没企图 2024-12-05 03:53:17

此处使用按位代替CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);

Use a bitwise or instead of and here CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文