C# CultureInfo.GetCultures 返回空列表
我无法使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您一定是在地板垫下的某个地方清扫了异常。
您的代码失败,因为
CultureTypes.AllCultures & ~CultureTypes.NeutralCultures
不起作用。您的列表包含中性文化和 new RegionInfo() 抛出。代码片段:
产生
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 andnew RegionInfo()
throws.The snippet:
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.
首先,最好是“调试”>“异常”并将 CLR 运行时异常设置为“抛出”。其次,我认为这段代码可能就是您正在寻找的:
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:
此处使用按位或代替和
CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
Use a bitwise or instead of and here
CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);