C# 应用程序中的资源和嵌入式资源有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1934826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:59:28  来源:igfitidea点击:

What's the difference between a Resource and an Embedded Resource in a C# application?

c#.netwpfresourcesembedded-resource

提问by Sergio Tapia

When should I use one or the other?

我什么时候应该使用其中一种?

I'd like all of the files I use in my app (images, sound, xml file, etc.) to be inside of the .exe file so I don't deploy with a bunch of folders and files.

我希望我在应用程序中使用的所有文件(图像、声音、xml 文件等)都位于 .exe 文件中,这样我就不会使用一堆文件夹和文件进行部署。

Thanks for the info.

谢谢(你的)信息。

采纳答案by viky

“Resource” and “Content” build actions are to access the WPF resources using the Uris. However “Embedded Resource” is for prior technologies. However both options embed the resource in assembly but “Resource” option to be used for WPF.

“Resource”和“Content”构建操作是使用 Uris 访问 WPF 资源。然而,“嵌入式资源”是针对现有技术的。然而,这两个选项都将资源嵌入到程序集中,但“资源”选项用于 WPF。

MSDN provides full explanation here.

MSDN在这里提供了完整的解释。

回答by kiamlaluno

As reported by MSDN,

据 MSDN 报道,

Embedded resources are the best choice if you have to share application resource (.resx) files between multiple projects. For example, if you have a common resource file that contains your company's logos, trademark information, and such, using embedded resources means you have to copy only the .resx file and not the associated resource data files.

You cannot edit embedded resources directly. If you try to edit an embedded resource, you will receive a message prompting you to convert the item to a linked resource in order to edit it. Conversion is recommended but optional. You must export them, make your modifications in an external program, and then import them back into your project.

如果您必须在多个项目之间共享应用程序资源 (.resx) 文件,则嵌入式资源是最佳选择。例如,如果您有一个包含公司徽标、商标信息等的公共资源文件,使用嵌入资源意味着您只需复制 .resx 文件,而不必复制关联的资源数据文件。

您不能直接编辑嵌入的资源。如果您尝试编辑嵌入资源,您将收到一条消息,提示您将该项目转换为链接资源以进行编辑。建议转换但可选。您必须导出它们,在外部程序中进行修改,然后将它们重新导入到您的项目中。

回答by Kent Boogaart

A WPF resource (build action = Resource) leverages embedded resources as supported by the core .NET framework, but adds support for accessing the embedded resource via a pack URI. From MSDN:

WPF 资源(构建操作 = Resource)利用核心 .NET 框架支持的嵌入式资源,但增加了对通过包 URI访问嵌入式资源的支持。从MSDN

WPF resource files are not the same as the embedded or linked type of resources that can be configured using the core .NET Framework support for assembly resources. While WPF resource files do leverage the core .NET Framework embedded resource support, the ability to access WPF resource files using pack URIs is easier than using namespaces.

WPF 资源文件与可以使用程序集资源的核心 .NET Framework 支持配置的嵌入或链接类型的资源不同。虽然 WPF 资源文件确实利用了核心 .NET Framework 嵌入式资源支持,但使用包 URI 访问 WPF 资源文件的能力比使用命名空间更容易。

回答by user2064543

Thanks for all the reports, that helped me find more precisely where was the problem: For me, it was the images used as project iconin the task bar that was built as resources and had to be built as content. All others images can be build as resources, no problem.

感谢所有报告,这些报告帮助我更准确地找到了问题所在:对我而言,任务栏中用作项目图标的图像是作为资源构建的,并且必须作为内容构建。所有其他图像都可以构建为资源,没问题。

Hope this helps for the future.

希望这对未来有所帮助。