[英]How to use shared resource file between projects in one solution?
I have a problem with resource files. 我的资源文件有问题。
I have a solution with two projects. 我有两个项目的解决方案。 The first project contains
ImageResource.resx
file with the images that I use. 第一个项目包含
ImageResource.resx
文件,其中包含我使用的图像。 Every Form
in this project can access this file from the designer. 此项目中的每个
Form
都可以从设计器访问此文件。 But I can see in the designer ImageResource.resx
file to use it from second project ( Reference to second project is present ). 但我可以在设计器
ImageResource.resx
文件中看到从第二个项目中使用它( 引用第二个项目 )。
I have added the ImageResource.resx
file as a link to my second project. 我已将
ImageResource.resx
文件添加为第二个项目的链接。 And I saw it in the designer! 我在设计师看到了它! But when I use an image from this resource in the second project Visual Studio modified my original file (It sets the namespaces, and other..) and my solution breaks.
但是当我在第二个项目中使用来自此资源的图像时,Visual Studio修改了我的原始文件(它设置名称空间,以及其他...) ,我的解决方案中断了。 Also Visual Studio tells me that
ImageResource.resx
is present in two dll's first_project.dll
and second_project.dll
Visual Studio还告诉我,
ImageResource.resx
存在于两个dll的first_project.dll
和second_project.dll
Can anybody help me with How to correctly use shared resources between projects? 任何人都可以帮助我如何在项目之间正确使用共享资源?
Next step consists of adding this icon to the other project(s). 下一步包括将此图标添加到其他项目。 Note the important difference, you need to add this icon as a link !
注意重要的区别,你需要添加这个图标作为链接 !
Adding as a link avoids the resource duplication. 添加为链接可避免资源重复。 Create a new Project within the same solution and name it eg Main .
在同一解决方案中创建一个新项目,并将其命名为Main 。 Create some folder in this new project, naming it Resources (the logical name for our purpose).
在这个新项目中创建一个文件夹,命名为Resources (我们的逻辑名称)。 Then right click on this folder, select Add Existing Item… and choose the image file from the shared project folder.
然后右键单击此文件夹,选择Add Existing Item ...并从共享项目文件夹中选择图像文件。 Make sure to use Add As Link here!
请务必在此处使用添加链接 ! If done correctly the icon of the newly added file will look slightly different (see below):
如果正确完成,新添加文件的图标将略有不同(见下文):
Added resource's icon must look like this 添加的资源图标必须如下所示
These are the five simple steps you must perform to share icons between projects. 这些是在项目之间共享图标必须执行的五个简单步骤。 You might ask "What are the benefits of this?"
你可能会问“这有什么好处?” The benefits are:
好处是:
This didn't work for me and I found another (VS2015+) approach. 这对我不起作用,我找到了另一种(VS2015 +)方法。 See https://stackoverflow.com/a/45471284/4151626
请参阅https://stackoverflow.com/a/45471284/4151626
In short, the shared project is directly included into the peripheral project. 简而言之,共享项目直接包含在外围项目中。 Thus, even though the IDE does not support
<Resource>
elements in the shared project. 因此,即使IDE不支持共享项目中的
<Resource>
元素。 <Resource>
elements can be added to the shared project, via a text editor. 可以通过文本编辑器将
<Resource>
元素添加到共享项目中。 They are then incorporated into the peripheral project during the build. 然后在构建期间将它们合并到外围项目中。
(Apologies for the hyper-link. I would just repost the answer for clarity, but the stackoverflow editors crack down on this, deleting duplicate answers to save you from ???.) (为超级链接道歉。我只是为了清晰起见重新发布答案,但stackoverflow编辑严厉打击这个,删除重复的答案以避免???。)
Can you use a symbolic link to share the file into multiple folders? 您可以使用符号链接将文件共享到多个文件夹中吗?
windows: 视窗:
mklink linked_location\ImageResource.resx original_location\ImageResource.resx
C:\Users\preet>mklink
Creates a symbolic link.
MKLINK [[/D] | [/H] | [/J]] Link Target
/D Creates a directory symbolic link. Default is a file
symbolic link.
/H Creates a hard link instead of a symbolic link.
/J Creates a Directory Junction.
Link specifies the new symbolic link name.
Target specifies the path (relative or absolute) that the new link
refers to.
If a resource file is really shared between projects, you should put it in a shared project. 如果资源文件确实在项目之间共享,则应将其放在共享项目中。
Solution 'Sample'
Project Sample.Data
Project Sample.Business
Project Sample.UI
Project Sample.Resource //shared resources are put in a shared project
You can't see the resource if it is not public , and it is default set to "Friend". 如果资源不公开 ,则无法查看该资源,并且默认设置为“朋友”。 You can set the "Access Modifier" in the Visual Designer (upper right-hand corner).
您可以在可视设计器(右上角)中设置“访问修改器”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.