我有一个MenuStrip控件,输入的所有翻译都是针对多种语言的。当我试图动态地改变文化时,所有的控件都在改变语言,因为它必须是除了MenuStrip之外的。这是我专门用于MenuStrip中的代码:异常本地化MenuStrip控制
protected ComponentResourceManager res2;
private void signOutToolStripMenuItem_Click(object sender, EventArgs e)
{
LoginForm login = new LoginForm();
if (login.ShowDialog() == DialogResult.OK)
{
//this is for other controls
resManager = new ComponentResourceManager(this.GetType());
this.ApplyResources(this, Thread.CurrentThread.CurrentCulture);
//this is for MenuStrip
res2 = new ComponentResourceManager(typeof(MenuStrip));
this.ApplyResourcesToolStripMenuItem(Thread.CurrentThread.CurrentCulture);
}
}
private void ApplyResourcesToolStripMenuItem(CultureInfo lang)
{
foreach (ToolStripItem m in this.menuStrip1.Items)
{
//resources.ApplyResources(m, m.Name, lang);
if (m is ToolStripMenuItem)
{
string text = res2.GetString(m.Name + ".Text", lang); //Exception is thrown here
if (text != null)
m.Text = text;
}
}
}
我使用其他开发人员的代码示例从这个论坛并作细微的变化。 现在,当我在运行时改变文化,我得到以下异常:
型“System.Resources.MissingManifestResourceException”的未处理的异常出现在mscorlib.dll的
其他信息:找不到任何适合于指定文化或中性文化的资源 。确保 “System.Windows.Forms.MenuStrip.resources”已正确嵌入,或者在编译时链接到程序集“System.Windows.Forms”中,或者 所需的所有附属程序集均可加载并完全签名。
我检查了资源文件和所有MenuStripItems都翻译/值有...
我没能找到在互联网解决方案,因此将非常感谢您的帮助。
在此先感谢!
我想知道你为什么手动执行_localization_,是不是设计器生成的代码?用户界面的BTW文化是CurrentUICulture,而不是CurrentCulture。 – 2015-04-02 11:44:03
阿德里亚诺,谢谢你的评论。关键在于,这是我第一次使用本地化的经验,我所做的是在设计时为每种语言设置了翻译,但单击一个按钮时,它应根据所选值进行更改。我找不到另一种解决方案... – 2015-04-02 11:54:03
本地化不仅仅是字符串,你应该将Thread.CurrentThread.CurrentUICulture(不是Thread.CurrentThread.CurrentCulture)设置为新文化,然后重新加载** Form。是的,你可以_assume_ WinForms控件不会缓存任何东西,你可能会忽略快捷方式,图像,布局,方向等,但这是一个很大的(而且是破碎的)假设。 – 2015-04-02 12:04:50