简体   繁体   English

混淆资源和GetManifestResourceNames()

[英]confused about resources and GetManifestResourceNames()

I've been learning about resources in C# and the visual C# IDE. 我一直在学习C#和visual C#IDE中的资源。 I'm confused now. 我现在很困惑。 I have read some pages on StackOverflow, like this one how-to-get-the-path-of-an-embebbed-resource and the documentation of Microsoft, but it confused me. 我已经阅读了StackOverflow上的一些页面,比如这个如何获取一个embebbed资源的路径和Microsoft的文档,但它让我很困惑。

My first question: what are resources: is it the .resources file or is it the files that are inside it, like icons. 我的第一个问题:什么是资源:它是.resources文件还是其中的文件,如图标。

second: When I use the GetManifestResourceNames method: do I get the .resources files names or the the names of the files inside it. 第二:当我使用GetManifestResourceNames方法时:我是否获得.resources文件名或其中的文件名。 When I use it in my program, I only get the .resources files, but reading topics like this loop-through-all-the-resources-in-a-resx-file , I get the impression I should get the names of the files inside the .resources file. 当我在我的程序中使用它时,我只获取.resources文件,但是阅读像这样循环遍历所有资源的resx文件的主题,我得到的印象我应该得到的名称.resources文件中的文件。

Is it me, or is this terminology really a bit confusing? 是我,还是这个术语真的有点令人困惑? Can anyone make it a little clearer? 任何人都可以让它更清晰一点吗? Thanks for all help. 谢谢你的帮助。

Resources are any file you compile by flagging it as an "EmbeddedResource" this simply merge the file into the assembly. 资源是您通过将其标记为“EmbeddedResource”来编译的任何文件,这只是将文件合并到程序集中。 GetManifestResourceNames() is just an enumerator that give us the name of all embedded compiled resources files, eg MyAssembly.resources . GetManifestResourceNames()只是一个枚举器,它为我们提供了所有嵌入式编译资源文件的名称,例如MyAssembly.resources The actual resource elements need to be enumerated via a ResourceSet which loads this resources file. 需要通过加载此资源文件的ResourceSet枚举实际资源元素。

I dont know if you still need the answer to this question but from my experience GetManifestResourceNames() returns the names of the .resource files embedded in your assembly. 我不知道你是否还需要这个问题的答案,但根据我的经验, GetManifestResourceNames()返回程序集中嵌入的.resource文件的名称。

If you want to access the individual resources you could so something along the lines of: 如果您想访问各个资源,您可以这样做:

Assembly assembly = Assembly.LoadFrom(assemblyName);
string[] names = assembly.GetManifestResourceNames();
ResourceSet set = new ResourceSet(assembly.GetManifestResourceStream(names[0]));
foreach (DictionaryEntry resource in set)
{
    Console.WriteLine("\n[{0}] \t{1}", resource.Key, resource.Value); 
}

I got my project to work because of Felice Pollano's answer. 由于Felice Pollano的回答,我得到了我的项目。 I added a folder to my solution called Images, and opened that folder in windows explorer, then copied my image file into the Images folder. 我在我的解决方案中添加了一个名为Images的文件夹,并在Windows资源管理器中打开该文件夹,然后将我的图像文件复制到Images文件夹中。 Then go into visual studio and click "show all files" at the top of the Solution Explorer, and right-clicked the image file in the Images folder and clicked Include in project. 然后进入visual studio并单击Solution Explorer顶部的“show all files”,右键单击Images文件夹中的图像文件,然后单击Include in project。 Then i left clicked the image file in the solution explorer, and in the Properties window, set the build action to Embedded Resource like you mentioned. 然后我左键单击解决方案资源管理器中的图像文件,然后在“属性”窗口中,将构建操作设置为嵌入式资源,就像您提到的那样。

Here is the code where I accessed this picture 这是我访问此图片的代码

private Dictionary<int, Image> GetImages()
{
  List<Stream> picsStrm = new List<Stream>();

  Assembly asmb = Assembly.GetExecutingAssembly();
  string[] picNames = asmb.GetManifestResourceNames();

  foreach (string s in picNames)
  {
    if (s.EndsWith(".png"))
    {
      Stream strm = asmb.GetManifestResourceStream(s);
      if (strm != null)
      {
        picsStrm.Add(strm);
      }
    }
  }

  Dictionary<int, Image> images = new Dictionary<int, Image>();

  int i = 0;

  foreach (Stream strm in picsStrm)
  {
    PngBitmapDecoder decoder = new PngBitmapDecoder(strm,
      BitmapCreateOptions.PreservePixelFormat,
      BitmapCacheOption.Default);
    BitmapSource bitmap = decoder.Frames[0] as BitmapSource;
    Image img = new Image();
    img.Source = bitmap;
    img.Stretch = Stretch.UniformToFill;
    images.Add(i, img);
    i++;

    strm.Close();
  }
  return images;
}

Which is actually from this article(A WCF-WPF Chat Application) by Islam ElDemery 这实际上来自Islam ElDemery的 这篇文章(一个WCF-WPF聊天应用程序)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM