【问题标题】:Looping through a set of image resources (.resx)循环访问一组图像资源 (.resx)
【发布时间】:2012-07-22 17:50:38
【问题描述】:

我在单独的 .resx 文件中有 3 组 9 张图片,我正在尝试弄清楚如何将一组图片循环成 9 个静态图片框。

Loop through all the resources in a .resx file

我查看了上面链接中的一些解决方案,例如使用ResXResourceReader,但是当我使用GetEnumerator 方法时出现解析错误。

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

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

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

谁能告诉我前两种方法做错了什么或者完全不同的东西?

【问题讨论】:

    标签: c# resources resx


    【解决方案1】:

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

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

    我找到了这个解决方案here

    【讨论】:

      【解决方案2】:

      查看 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
           } 
        }
      

      更多内容请关注here。

      【讨论】:

      • 当它查看我查看的 resx 文件中的第一个图像时,我一直遇到解析错误。
      • string resxFile = @"//以Theme1Images.resx结尾的文件路径";使用(ResXResourceReader resxReader = new ResXResourceReader(resxFile)){foreach(resxReader中的DictionaryEntry条目){string resourceKey = entry.Key.ToString();对象资源 = entry.Value; MessageBox.Show("message");} } 这一切都在 try-catch 中:ArgumentException 未处理 ResX 文件找不到路径“//FilePath\bin\resources\1336578195503.jpg”的一部分。第 123 行,位置 5。无法解析。
      • @Jamz 该文件存在于 \bin\resources 文件夹中吗?
      猜你喜欢
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多