vb.net 如何在 Visual Basic 中获取资源文件值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5639582/
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
How do I get resource file values in Visual Basic?
提问by Biff MaGriff
I'm new to Visual Basic, and I'm having issues accessing the resource file for my project.
我是 Visual Basic 的新手,在访问我的项目的资源文件时遇到了问题。
Dim rm As Resources.ResourceManager = New Resources.ResourceManager("MyProjectName.My.Resources.Resources", [Assembly].GetExecutingAssembly())
Dim myValue = rm.GetString(lookUpKey) 'boom Object reference not set to an instance of an object.
I think the issue is with the string "MyProjectName.My.Resources.Resources".
我认为问题出在字符串“MyProjectName.My.Resources.Resources”上。
Would I be better off moving the strings into their own resource file?
将字符串移动到它们自己的资源文件中会更好吗?
采纳答案by Biff MaGriff
I was unable to access the resource file until I moved the .resx file into its own project and referenced that project from my main one. I also had to create a dummy class in that project so that it would compile into a DLL file.
在我将 .resx 文件移动到它自己的项目中并从我的主要项目中引用该项目之前,我无法访问资源文件。我还必须在该项目中创建一个虚拟类,以便将其编译为 DLL 文件。
The code for accessing the resource file is actually located in the generated Resource.resx.vb file.
访问资源文件的代码实际上位于生成的Resource.resx.vb文件中。
I was able to access the resource file using the following code.
我能够使用以下代码访问资源文件。
'Name of Class Library where I moved the resx file
Dim classLibraryName As String = "ResourceProj"
'Name of Resource File without the .resx suffix
Dim resourceFileName As String = "Mappings"
'Finding the assembly of the resx file, ResourceProjClass is a dummy class I created so that the dll would build.
Dim myAssembly As Assembly = GetType(ResourceProj.ResourceProjClass).Assembly
Dim rm As Resources.ResourceManager = Nothing
rm = New Resources.ResourceManager(classLibraryName & "." & resourceFileName, GetType(myAssembly)
Return rm.GetString(lookUpKey)
回答by Freesn?w
I thought it was something similar to:
我认为它类似于:
my.Resource.whateverhere
Is that not the kind of resources you are looking for?
这不是您要寻找的资源吗?
回答by Makah
Try
尝试
Global.<MyNamespace>.My.Resources.<ResourceStringName>
to access Resource Strings
访问资源字符串
回答by akuma6099
If you load an external .resx file and want it to show up under intellisense My.Resources then you need to do 2 things.
如果您加载外部 .resx 文件并希望它显示在智能感知 My.Resources 下,那么您需要做两件事。
First the file should be in the root of your project. Simply right click the project and do "Add Existing Item", and give it your .resx file. You should notice that there is no chevron to expand the resx file like your built-in resource file.
首先,该文件应位于项目的根目录中。只需右键单击该项目并执行“添加现有项目”,然后为其提供您的 .resx 文件。您应该注意到,没有像内置资源文件那样扩展 resx 文件的 V 形符号。
The last step is to highlight your resx file and go to the properties window. Under Custom Tool put "ResXFileCodeGenerator" and under the Custom Tool NameSpace put "My.Resources". You should now be able to programmatically access this resource under My.Resources.[name of the resx file].resource_item.
最后一步是突出显示您的 resx 文件并转到属性窗口。在自定义工具下放置“ResXFileCodeGenerator”,在自定义工具命名空间下放置“My.Resources”。您现在应该能够以编程方式访问 My.Resources.[resx 文件的名称].resource_item 下的此资源。
回答by Priyank
Refer to the MSDN article Retrieving Resources with the ResourceManager Classfor naming convetions:
有关命名约定,请参阅 MSDN 文章Retrieving Resources with the ResourceManager Class:
Dim myManager As New _
System.Resources.ResourceManager("ResourceNamespace.myResources", _
myAssembly)
回答by manji
Try ResourceManager("MyProjectName.Resources", ...)
, otherwise if it's the application resources you can simply use My.Resources.HD
(see here:My.Resources Object)
尝试ResourceManager("MyProjectName.Resources", ...)
,否则如果它是您可以简单使用的应用程序资源My.Resources.HD
(请参阅此处:My.Resources Object)
or
或者
Open Reflector, load your assembly there, go to resources, a list of resources appears, search for the one containg 'HD'
, copy the name (it's like MyProjectName.Resources.resources), remove the last .resources
and try with that.
打开Reflector,在那里加载你的程序集,转到资源,一个资源列表出现,搜索一个包含的'HD'
,复制名称(就像MyProjectName.Resources.resources),删除最后一个.resources
并尝试使用它。
回答by Pierre-David Sabourin
Simply:
简单地:
Dim loginMessage As String = Global.Resources.NameOfYourResxFile.NameOFVariable