如何获取资源文件中所有资源的名称 [英] How to get the names of all resources in a resource file

查看:312
本文介绍了如何获取资源文件中所有资源的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Basic项目中,我添加了一个包含一堆图像的资源文件(resx).

Within a Visual Basic Project I have added a resource file (resx) that contains a bunch of images.

现在,我想查询图像的名称. 如果我在Visual Studio IDE的设计器视图中打开resx文件并选择图像,则属性网格会显示一个name属性(默认为不带扩展名的文件名,但可以更改).

Now I want to query the names of the images. If I open the resx file in the designer view in the Visual Studio IDE and select an image, the property grid shows me a name property (defaults to "filename without extension but can be changed).

背景是我有一个图像列表,该图像列表是在运行时创建的,并用资源文件中的图像填充.为了能够通过按键访问这些图像,我必须对其进行设置.

The background is that I have a imagelist that is created at runtime and populated with the images from the resource file. To be able to access these images by the key, I have to set it.

我的代码如下(所有硬编码):

My code looks like this (everything hard coded):

Dim imagelist as new Imagelist
imageList.Images.Add("A", My.Resources.MyImages.A)
imageList.Images.Add("B", My.Resources.MyImages.B)
imageList.Images.Add("C", My.Resources.MyImages.C)
imageList.Images.Add("D", My.Resources.MyImages.D)
imageList.Images.Add("E", My.Resources.MyImages.E)
....
imageList.Images.Add("XYZ", My.Resources.MyImages.XYZ)

我想做到这一点:

Dim imagelist as new ImageList

For Each img in GetMeAllImagesWithNameFromMyResourceFile
    imageList.Images.Add(img.Name, img.ImageFile)
Next

其中Name是一个字符串,ImageFile是一个System.Drawing.Bitmap

where Name is a string and ImageFile a System.Drawing.Bitmap

推荐答案

看看这段代码是否有帮助.

See if this piece of code helps.

    Dim runTimeResourceSet As Object
    Dim dictEntry As DictionaryEntry

    runTimeResourceSet = My.Resources.ResourceManager.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, False, True)
    For Each dictEntry In runTimeResourceSet
        If (dictEntry.Value.GetType() Is GetType(Icon)) Then
            Console.WriteLine(dictEntry.Key)
        End If
    Next

我以Icon为例,如果您使用Bitmap,则必须进行更改.

I have used Icon as an example, which you will have to change if you are using Bitmap.

您将不得不使用dictEntry.Value&的引用.了解如何将其添加到图像列表中.

You will have to use reference of dictEntry.Value & see how it can be used for adding it to imagelist.

这篇关于如何获取资源文件中所有资源的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆