0

有没有办法直接访问 .resx 中的字符串?

我有几个不同的项目,.resx 是使用 SharpDevelop 模板“Misc>>Empty resource file”创建的。

网上的大部分方法我都试过了,比如:

... = new ResourceManager ("ProjectEtc.resxFileName", 
           Assembly.GetExecutingAssembly)

但我收到同样的错误:

Could not find any resources appropriate for the specified culture or 
neutral culture. Make sure "ProjectEtc.resxFileName.resources" was 
correctly embedded or linked into assembly "Project calling" at compile time...

不管我做什么。

看来我需要将我的 .resx 也转换为 .resource 文件,我不想这样做。

我是否错过了使用#develop 模板的一些步骤?

等一下……这甚至是在 .NET 中存储常量字符串的最佳方式吗?

谢谢,戴恩

4

1 回答 1

0

检查 .resx 文件的Build 操作设置为EmbeddedResource。当您使用 Empty 资源文件模板创建新文件时,SharpDevelop 默认应该这样做,但值得检查。

现在您需要将项目的根命名空间和不带文件扩展名的 resx 文件名传递给 ResourceManager 构造函数。

因此,如果您在 SharpDevelop 中创建一个名为 TestResource 的新项目,我的根命名空间将是 TestResource。然后,如果您将一个名为 Foo.resx 的资源文件添加到您的项目中。然后添加一个名为“MyString”的字符串,然后您可以使用以下代码访问相应的字符串值:

Dim resourceManager As New ResourceManager("TestResource.Foo", Assembly.GetExecutingAssembly)
Dim result = resourceManager.GetString("MyString")

当您编译应用程序时,.resx 文件将被编译成 .resources 文件并嵌入到您的项目中。如果您使用ILSpy 之类的工具,您可以看到这个嵌入式资源以及它的名称。

使用 .resx 文件通常是在 .NET 中执行多语言应用程序的方式,因此使用它们来存储字符串是可以的。

于 2012-02-07T19:43:07.717 回答