C# 资源(.resx)文件有什么好处?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1134018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:01:50  来源:igfitidea点击:

What are the benefits of resource(.resx) files?

c#.netvb.netresx

提问by Newbie

What compelling reasons exist for using them?

使用它们有哪些令人信服的理由?

采纳答案by womp

  • Resource files give you an easy way to localize/internationalize your .net applications by automatically determining which language resx file to use based on the user's locale. To add more languages, simply add another translated resource file.

  • Resource files give you a central location to store your strings, files and scripts and refer to them in a strongly-typed manner (so the compile will break if you reference them improperly).

  • Resource files can be compiled into satellite assemblies, making it easy to change up the resources in a production application without having to recompile the whole thing.

  • 通过根据用户的区域设置自动确定要使用的语言 resx 文件,资源文件为您提供了一种本地化/国际化 .net 应用程序的简单方法。要添加更多语言,只需添加另一个已翻译的资源文件。

  • 资源文件为您提供了一个中央位置来存储您的字符串、文件和脚本,并以强类型方式引用它们(因此如果您不正确地引用它们,编译就会中断)。

  • 资源文件可以编译为附属程序集,从而可以轻松更改生产应用程序中的资源,而无需重新编译整个程序。

回答by Shiraz Bhaiji

With resx you can have one per language (that is spoken language not programming language) enabling your program / system to be multilingual.

使用 resx,您可以为每种语言(即口语而不是编程语言)提供一种语言,从而使您的程序/系统能够使用多种语言。

回答by Steven Sudit

As a supplement to the other answers, string resources are for human-readable text, not constants that will be used programmatically. They're great for error messages, button labels and the like.

作为对其他答案的补充,字符串资源用于人类可读的文本,而不是以编程方式使用的常量。它们非常适合错误消息、按钮标签等。

Very often, rather than the final string, we store a format string so that variables can be substituted in at the last moment. The nice thing about this method is that, unlike concatenation, it's not broken when a language has different word order requirements.

很多时候,我们存储一个格式字符串,而不是最后的字符串,以便在最后时刻可以替换变量。这种方法的好处在于,与串联不同,当一种语言有不同的词序要求时,它不会被破坏。

回答by Brian

Resource files enable you to change the text/graphics your program will display without editting the code of the program itself. For many reasons, it is often considered ideal to avoid needing to edit your program's source code in order to make changes that are not part of your application's logic.

资源文件使您能够更改程序将显示的文本/图形,而无需编辑程序本身的代码。出于多种原因,通常认为最好避免需要编辑程序的源代码来进行不属于应用程序逻辑的更改。

回答by Cameron

In addition to the answers above, resource files can also provide you an place to store test files for performing Read/Write tests. That way, regardless of how other people's environment is set up, you can have a place to store logs or other files.

除了上面的答案,资源文件还可以为您提供一个存储测试文件的地方,用于执行读/写测试。这样,无论其他人的环境如何设置,您都可以有一个地方来存储日志或其他文件。

Keep in mind that what files you do place in resources must be serializable. Check below for more info:

请记住,您在资源中放置的文件必须是可序列化的。查看下面的更多信息:

https://msdn.microsoft.com/en-us/library/f45fce5x(v=vs.80).aspx

https://msdn.microsoft.com/en-us/library/f45fce5x(v=vs.80).aspx