【问题标题】:How do I get resource file values in Visual Basic?如何在 Visual Basic 中获取资源文件值?
【发布时间】:2011-08-04 02:51:16
【问题描述】:

我是 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.

我认为问题出在字符串“MyProjectName.My.Resources.Resources”上。

将字符串移动到它们自己的资源文件中会更好吗?

【问题讨论】:

  • 不,它是“HD”我的项目资源文件中的字符串名称。 *编辑将“值”更改为“名称”。
  • 你有反射器吗?在那里打开你的程序集,转到资源,出现资源列表,搜索包含HD的资源,复制名称(就像MyProjectName.Resources.resources),删除最后一个.resources并尝试。

标签: vb.net winforms embedded-resource


【解决方案1】:

如果您加载一个外部 .resx 文件并希望它显示在 intellisense My.Resources 下,那么您需要做 2 件事。

首先,该文件应位于项目的根目录中。只需右键单击该项目并执行“添加现有项目”,然后将其提供给您的 .resx 文件。您应该注意到,没有像您的内置资源文件那样扩展 resx 文件的 V 形。

最后一步是突出显示您的 resx 文件并转到属性窗口。在自定义工具下放置“ResXFileCodeGenerator”,在自定义工具命名空间下放置“My.Resources”。您现在应该能够以编程方式访问 My.Resources.[resx 文件的名称].resource_item 下的此资源。

【讨论】:

    【解决方案2】:

    简单地说:

    Dim loginMessage As String = Global.Resources.NameOfYourResxFile.NameOFVariable
    

    【讨论】:

    • 请通过添加 4 个前导空格来格式化您的代码。另外,代码有什么作用?我对VB一无所知。
    • 在 c 中然后:string loginMessage = Global.Resources.NameOfYourResxFile.NameOFVariable;
    【解决方案3】:

    在我将 .resx 文件移到它自己的项目中并从我的主项目中引用该项目之前,我无法访问资源文件。我还必须在该项目中创建一个虚拟类,以便将其编译为 DLL 文件。

    访问资源文件的代码实际上位于生成的Resource.resx.vb文件中。

    我能够使用以下代码访问资源文件。

    '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)
    

    【讨论】:

      【解决方案4】:

      有关命名约定,请参阅 MSDN 文章 Retrieving Resources with the ResourceManager Class

      Dim myManager As New _
         System.Resources.ResourceManager("ResourceNamespace.myResources", _
         myAssembly)
      

      【讨论】:

        【解决方案5】:

        试试

         Global.<MyNamespace>.My.Resources.<ResourceStringName>
        

        访问资源字符串

        【讨论】:

        • 谢谢你。它让我非常接近答案。我不得不使用“Global..My.Resources.RESOURCEMANAGER.GETSTRING(ResourceStringName)”
        【解决方案6】:

        试试ResourceManager("MyProjectName.Resources", ...),否则如果是应用程序资源你可以直接使用My.Resources.HD(见这里:My.Resources Object

        打开Reflector,在那里加载你的程序集,转到资源,出现资源列表,搜索包含'HD'的资源,复制名称(就像MyProjectName.Resources.资源),删除最后一个.resources 并尝试使用它。

        【讨论】:

          【解决方案7】:

          我认为它类似于:

          my.Resource.whateverhere
          

          这不是您要寻找的资源吗?

          【讨论】:

          • 我的 2 美分,我必须用来访问我的资源的代码是 Me.GetLocalResourceObject("[ResourceKey]")。注意使用GetLocalResourceObject是因为资源文件所在的文件夹是Local_Resources
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-06-09
          • 1970-01-01
          • 1970-01-01
          • 2019-06-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多