Windows 窗体中图像资源的本地化
我正在使用 C# 3.5 和 Windows 窗体 (以及 Telerik 组件(如果重要的话)。如何获得一个按钮,以三种(或 N..)种不同的语言使用三张不同的图片(一张用于空闲,一张用于鼠标悬停,一张用于单击)?
当我添加资源图像(例如 JPEG 文件)时,我可以使用资源管理器来获取它,一切都很棒。我还可以添加另一个图像(同时使用不同的名称),然后更改文件的语言并更改图片。
现在的问题是: 如果我不能给它们相同的名称(resource.resx 中的 egajpg、resources-foo.resx 中的 a.jpg 等),如何将每种语言的按钮图像更改为正确的图像。
我认为在代码中检查当前文化,然后通过使用 OnMouseEnter/Leaves/Down 中的图片资源名称来实现三张图片之间的切换是不现实的。
由于平均应用程序有 100 多个按钮,因此必须有一种简单易用的方法来做到这一点,但我只是想不到(或找不到......)...
I'm using C# 3.5 and Windows Forms (and Telerik components if it matters). How can I get a button to use three different pictures (one for idle, one for mouse hover and one for while clicked) in three (or N..) different languages?
When I'm adding a resource image (for example, a JPEG file), I can use the resource manager to get it and everything is great. I can also add another image (while using a different name) and then change the language of the file and change the picture.
Now the question:
How can I change the button image in each language to the correct image if I can't give them the same name (e.g. a.jpg in resource.resx, a.jpg in resources-foo.resx, etc).
I don't think it is realistic to check in the code the current culture and then by using the pictures resources names in the OnMouseEnter/Leaves/Down implement the switch between the three picture.
As an average application has 100+ buttons there must be a simple and easy way to do it, that I just can't think of (or find...)...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.NET 中的 ResourceManager 不仅仅进行字符串本地化。它可以提供任何类型的本地化二进制内容,包括图像。这意味着您永远不需要编写逻辑来选择图像的相关本地化版本,甚至不需要操作图像文件路径。您只需要求 ResourceManager 提供您想要的图像,它就会为您提供该图像的适当语言版本(取决于当前的 UICulture)。
Visual Studio 可以轻松地将图像添加到资源文件中。资源编辑器在左上角有一个按钮(见下文),可让您查看 resx 文件中包含的不同类型的资源,这些资源可以是字符串(我们大多数人都熟悉的最常见选择)、图像和更多的。
此 MSDN 链接 解释了整个事情。这是我从中获取的一个示例,它展示了如何从资源中检索本地化图像:
The ResourceManager in .NET does not only do string localization. It can provide localized binary content of any kind, including images. This means that you will never need to write logic to pick the relevant localized version of an image, and won't even need to manipulate image file paths. You just ask the ResourceManager to give you the image you want, and it will give you the appropriate language version of this image (depending on the current
UICulture
).Visual Studio makes it easy to add images to a resource file. The Resource editor has a button in the top left corner (see below) that lets you view the different types of resources contained in your resx file, which can be strings (the most common choice that most of us are familiar with), images and more.
This MSDN link explain the whole thing. Here's one example I took from it which shows how to retrieve a localized image from your resources:
我会使用资源文件。他们的工作原理如下:“如果使用这种语言,则加载“Button1A.jpg”,如果使用其他语言,则加载“Button1B.jpg””,等等。我不确定语法或如何实现它们,但我我非常确定您可以在某处指定语言/区域设置(或者可能会根据 Windows 设置为您指定),并且您只需提供何时使用哪个图像,它就会为您完成其余的工作。
一定要查找资源文件。
我认为资源文件的默认用途是字符串。因此,它们不应该加载不同的图像,而应该用于加载不同的翻译(本地化)字符串。但是,与此不同,只需提供不同的文件路径(不同图像的文件路径)来代替字符串。
希望我说得有道理,抱歉没有提供例子。
I would use resource files. They work something like this: "if using this language, load 'Button1A.jpg', if using other language, load 'Button1B.jpg'", etc. I'm not sure of the syntax or how to implement them, but I'm pretty sure you can specify a language/locale somewhere (or maybe it gets specified for you based on Windows settings), and you just provide which image to use when and it'll do the rest for you.
Definitely look up resource files.
I think the default use for resource files is for strings. So rather than loading different images, they are supposed to be used to load different translated (localized) strings. However, rather than this, just supply a different filepath (the filepath to the different images) in place of the strings.
Hopefully I've made sense, sorry for not providing an example.