如何检测 WinForms 中的可本地化属性?
在其他地方,有人指出“控件的“Visible”属性无法本地化。这句话只对了一半。但是,“Visible”属性的 LocalizedAttribute 设置为 true。但这仅仅意味着该属性被序列化到资源文件中。我编写了一个测试程序,将不变文化的标签可见属性设置为“false”。使表单可本地化,我将德国文化的可见属性更改为“true”。现在,如果我在系统设置为德语区域设置的情况下启动程序,会发生什么情况?标签保持不可见。 检查资源文件 Form1.de.resx 可以看到可见属性尚未序列化。但如果我手动将其添加到资源文件中:
<data name="label1.Visible" type="System.Boolean, mscorlib">
<value>True</value>
</data>
就会出现标签。我承认有点困惑。 两个问题:
- 如何使用内置序列化器检测属性是否“真正”可本地化?
- 覆盖默认行为的推荐方法是什么?
编辑: 也许我需要澄清我的问题。 我的示例程序是一个简单的形式,其默认语言设置为不变。我已通过表单设计器手动添加了德语资源。程序在区域设置设置为德语的系统上运行。
案例 1
添加到表单的面板的“可见”属性:
1.) 在不变文化中设置为 false,在德国文化中设置为 true(默认)。 =>面板不可见
2.) 在不变文化中设置为 true,在德国文化中设置为 false。 =>面板不可见(按预期工作)
显然,如果该值不是默认值,则仅写入特定于语言的资源文件。
案例 2
添加到表单的标签的“字体”属性:
1.) 在固定文化中设置为粗体,在德国文化中属性重置为默认值。 =>标签不是粗体
2.) 在固定文化中设置为默认值,在德国文化中设置为粗体。 =>标签为粗体
现在这里的属性已按预期序列化。
这是一个错误还是我错过了什么?
Somewhere else, someone states that the "Visible" Property of a control cannot be localized. This is half-true. However, the "Visible" Property has the LocalizableAttribute set to true. But this just means that the property is serialized to the resource file. I wrote a test program that has the visible property of a label set to "false" for the Invariant Culture. Making the form localizable I changed the visible property to "true" for the German culture. Now what happens if I start the program with the system set to German locale? The label stays invisible.
Checking the resource file Form1.de.resx I can see that the visible property has not been serialized. But if I manually add this to the resource file:
<data name="label1.Visible" type="System.Boolean, mscorlib">
<value>True</value>
</data>
the label appears. I confess to be slightly confused.
Two questions:
- How can I detect if a property is "truly" localizable using the build-in serializer?
- What is the recommended way to override the default behaviour?
Edit:
Maybe I need to clarify my question.
My sample program is a simple form which has the default language set to invariant. I have manually added german resources through the forms designer. Program runs on a system with regional settings set to german.
Case 1
"Visible" Property of a panel added to the form:
1.) set to false in Invariant culture, true (default) in german culture. => panel is invisible
2.) set to true in Invariant culture, false in german culture. => panel is invisible (works as expected)
Apparently the value is only written to the language specific resource file if it's not the default.
Case 2
"Font" Property of a label added to the form:
1.) set to bold in Invariant culture, Property is reset to default in german culture. => label is not bold
2.) set to default in Invariant culture, bold in german culture. => label is bold
Now here the properties are serialized as expected.
Is it a bug or am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我们已收到微软的回复:
所以答案是:目前没有解决办法。
We have received a response from Microsoft:
So the answer is: There is currently no solution.
当我尝试时,效果很好。测试步骤:
像这样修改构造函数:
当我运行表单时,文本框不可见。我注释掉 CurrentUICulture 作业以切换回英语:文本框可见。
This works just fine when I try it. Steps taken to test:
modified the constructor like this:
The text box isn't visible when I run the form. I comment out the CurrentUICulture assignment to switch back to English: text box is visible.
我认为微软不会将此归类为错误。它按照设计工作;但这绝对是一个令人头痛的事情。
据我所知,没有一个简单的解决方案可以对 Label 控件进行子类化并添加属性 - 恶心。
作为解决方法,您可以简单地设置宽度或文本属性而不是可见性吗?
I don't think Microsoft would classify this as a bug. It's working as designed; but it definitely is a pain in the neck.
There's not an easy solution that I'm aware of short of sub-classing the Label control and adding properties - yuck.
As a workaround, could you simply set the Width or Text properties instead of the Visibility?
我仍然相信 Visible 属性不可本地化。当我切换到默认语言然后返回到特定语言时,它总是重置为默认语言的设置,无论它是否是默认设置。
I still believe that the Visible property is not localizable. It is always reset to its setting for the default language when I switch to the default laguage and then back to the specific language, does not matter whether it is a default setting or not.