【发布时间】:2017-12-12 18:17:20
【问题描述】:
我在一个小型测试应用程序中使用 ComponentResourceManager,因为我需要修复以前在现有的、更大的 Winforms 应用程序中自动生成的本地化,以便手动实现——以便手动插入的所有字符串每当有人打开表单设计器时,都不会对资源文件进行攻击。
为了这个测试,表单上有一个按钮控件。我想使用存储在资源文件中的按钮属性来设置按钮属性。如果我寻找属性并直接设置它们,这很好。
ResourceManager resourceManager = new ResourceManager("LocalizationTest1.buttonProps", typeof(Form1).Assembly);
button1.Text = resourceManager.GetString("button1.Text");
button1.Location = (Point)(resourceManager.GetObject("button1.Location"));
但是,如下尝试使用 ApplyResources 不起作用。
ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(Form1));
componentResourceManager.ApplyResources(this.button1, "button1");
在资源设计器中资源名称 button1.Text 和 button1.Location 旁边都有 the resource name 'button1...' is not a valid identifier 也可能是相关的。
到目前为止,我还没有在网上找到任何相关的东西,所以任何可能有帮助的想法都将不胜感激。
【问题讨论】:
-
对我来说,it works。在应用资源以查看结果之前,您是否改变了文化?你确定你的表单是localizable?
-
该表单是可本地化的,因为如果我更改 CurrentUICulture,它会将语言更改为适当的 resx 文件,但前提是我直接处理属性;即
button1.Text = resourceManager.GetString("button1.Text");。并且不尝试使用 ApplyResources 更广泛地解决该对象 -
你试过我在example分享的确切代码吗?
-
我有,不幸的是它也不适合我。在这个时刻,我几乎认为这是我在资源名称上做错了。
-
您是自己手动创建资源吗?
标签: c# winforms localization