简体   繁体   English

如何在一个解决方案中的项目之间使用共享资源文件?

[英]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.dllsecond_project.dll

Can anybody help me with How to correctly use shared resources between projects? 任何人都可以帮助我如何在项目之间正确使用共享资源?

  1. The correct way to share resources is to create a global shared project. 共享资源的正确方法是创建全局共享项目。 Create a new Project with the name Resources : 创建一个名为Resources的新项目:

解决方案资源管理器,显示示例解决方案,其中包含名为* Resources *的项目

  1. Next we must add some resources (such as icons) to the project. 接下来,我们必须向项目添加一些资源(例如图标)。 Do this as usual. 像往常一样这样做。 Go to the projects setting, select tab Resources and Add Existing File… to the project. 转到项目设置,选择选项卡资源添加现有文件...到项目。 We can see that the icon is added to the project and was copied to the local folder: 我们可以看到该图标已添加到项目中并被复制到本地文件夹:

例如,Resources项目的Resources选项卡显示了一只小狗的图片,Solution Explorer显示Resources项目的Resources文件夹包含了小狗的图片

  1. 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): 如果正确完成,新添加文件的图标将略有不同(见下文):

在* Add Existing Item *对话框中,从* Add *按钮的下拉列表中选择* Add As Link *选项

Added resource's icon must look like this 添加的资源图标必须如下所示

在解决方案资源管理器中,小狗资源的主项目图标在图标上有一个箭头,而资源项目的小狗资源图标在图标上没有箭头

  1. Now we must set the Build Action for this file to None . 现在我们必须将此文件的Build Action设置为None For this select the file and go to the Properties Window. 为此,请选择文件并转到“ 属性”窗口。 There choose None for Build Action . 对于Build Action,选择None We need to do this to avoid embedding this icon into the assembly: 我们需要这样做以避免将此图标嵌入到程序集中:

在解决方案资源管理器中,更改* Main *项目的puppy资源的属性。选择*无*的构建操作

  1. Finally we need to add the linked files to the Resources of the corresponding project. 最后,我们需要将链接文件添加到相应项目的Resources中。 Open the project Properties for the project where we just added the files. 打开我们刚刚添加文件的项目的项目属性 Choose the Resource tab and drag the linked file there: 选择Resource选项卡并将链接文件拖到那里:

将链接图像从解决方案资源管理器的* Main *项目资源拖动到* Main *项目的资源选项卡的查看窗格

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: 好处是:

  • We store resources in one place, and 我们将资源存储在一个地方,并且
  • It is easy to replace an icon with a new one. 用新图标替换图标很容易。

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.

相关问题 如何在解决方案中的多个项目之间引用/使用资源字典 - How to reference/use Resource Dictionary between multiple projects inside solution JSON Config文件在同一解决方案中的项目之间共享 - JSON Config file shared between projects in same solution 解决方案中的项目之间如何共享单个配置? - How can a single configuration be shared between projects in a solution? 如何在一个解决方案中共享源项目之间的许多二进制文件? - How to share many binary files between source projects in one solution? xml注释如何在一个解决方案中的项目之间工作 - How does xml comments work between projects in one solution Gloabal Json文件在一个解决方案中与许多项目共享(asp.net core 2.0) - Gloabal Json file shared with many projects in one solution (asp.net core 2.0) 如何在一个解决方案中从 2 个项目中的同一个 txt 文件中获取数据 - How to get data from same txt file in 2 projects in one solution 一个解决方案中两个项目之间的继承 - Inheritance between two projects in one solution 如何在Visual Studio中的项目之间共享资源文件? - How can I share a resource file between projects in Visual Studio? 如何在多项目解决方案中添加共享资源? - How to add a shared resource in a multi project solution?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM