循环使用一组图像资源(.resx)



我在单独的.resx文件中有3组9个图像,我正在试图找出如何将单个图像循环到9个静态图片框中。

循环浏览.resx文件中的所有资源

我已经查看了上面链接中的一些解决方案,比如使用ResXResourceReader,但在使用GetEnumerator方法时会出现解析错误。

当我使用ResourceSet resourceSet = MyResourceClass.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);行时,Form类中没有ResourceManager的定义,或者当我创建自己的ResourceManager时没有GetResourceSet方法的定义。

实际上有一个叫CreateFileBasedResourceManager的方法,我已经涉猎过了,但说实话,除了目录之外,我对它所需要的参数不太了解。

我还研究了一些涉及程序集和在运行时检索正在执行的映像程序集的解决方案,但我认为这有点超出了我的深度。

有人能告诉我前两种方法做错了什么吗?或者可能是完全不同的方法吗?

查看MSDN,您应该能够迭代RESX文件中的值,如下所示:

string resxFile = @".CarResources.resx";

// Get resources from .resx file.
  using (ResXResourceSet resxSet = new ResXResourceSet(resxFile))
  {
     // Retrieve the image.
     Object image =  resxSet.GetObject("NAMEOFFILE", true);
  }

如果你想迭代RESX文件中的所有对象,你可以这样做:

using (ResXResourceReader resxReader = new ResXResourceReader(resxFile))
  {
     foreach (DictionaryEntry entry in resxReader) {
        // entry.Key is the name of the file
        // entry.Value is the actual object...add it to the list of images you were looking to keep track of
     } 
  }

更多信息可以在这里找到。

我知道这是一个老问题,但今天我遇到了同样的问题,并解决了设置BasePath属性的问题,如下所示:

oResReader = new ResXResourceReader(strInputFile);
oResReader.BasePath = Path.GetDirectoryName(strInputFile);

我在这里找到了这个解决方案

最新更新