How to use shared resource file between projects in one solution?

后端 未结 5 1154
忘了有多久
忘了有多久 2020-12-05 07:25

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

相关标签:
5条回答
  • 2020-12-05 07:53

    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.
    
    0 讨论(0)
  • 2020-12-05 07:57
    1. The correct way to share resources is to create a global shared project. Create a new Project with the name Resources:

    Solution Explorer, showing an example solution, which contains a project named *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:

    For example, the Resources project's Resources tab shows a picture of a puppy, and the Solution Explorer shows that the Resources project's Resources folder includes the picture of the puppy

    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 e.g. Main. Create some folder in this new project, naming it Resources (the logical name for our purpose). Then right click on this folder, select Add Existing Item… and choose the image file from the shared project folder. Make sure to use Add As Link here! If done correctly the icon of the newly added file will look slightly different (see below):

    In the *Add Existing Item* dialog, choose the *Add As Link* option from the *Add* button's drop-down list

    Added resource's icon must look like this

    In Solution Explorer, the Main project's icon for the puppy Resource has an arrow on the icon, whereas the Resources project's icon for the puppy Resource does not have an arrow on the icon

    1. Now we must set the Build Action for this file to None. For this select the file and go to the Properties Window. There choose None for Build Action. We need to do this to avoid embedding this icon into the assembly:

    In Solution Explorer, change the *Main* project's puppy resource's properties. Choose a Build Action of *None*

    1. Finally we need to add the linked files to the Resources of the corresponding project. Open the project Properties for the project where we just added the files. Choose the Resource tab and drag the linked file there:

    Drag the linked image from the Solution Explorer's *Main* project's resources to the *Main* project's resources tab's viewing pane

    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.
    0 讨论(0)
  • 2020-12-05 08:03

    This didn't work for me and I found another (VS2015+) approach. See 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. <Resource> elements can be added to the shared project, via a text editor. 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 ???.)

    0 讨论(0)
  • 2020-12-05 08:03

    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  
    
    0 讨论(0)
  • 2020-12-05 08:13

    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).

    0 讨论(0)
提交回复
热议问题