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

发布于 2024-09-28 06:54:26 字数 543 浏览 0 评论 0原文

我的资源文件有问题。

我有两个项目的解决方案。第一个项目包含 ImageResource.resx 文件以及我使用的图像。此项目中的每个 Form 都可以从设计器访问此文件。但我可以在设计器 ImageResource.resx 文件中看到从第二个项目使用它(存在对第二个项目的引用)。

我已添加 ImageResource.resx 文件作为我的第二个项目的链接。我在设计师身上看到了这一点!但是,当我在第二个项目中使用此资源中的图像时,Visual Studio 修改了我的原始文件(它设置了命名空间和其他..),并且我的解决方案失败了。 Visual Studio 还告诉我,ImageResource.resx 存在于两个 dll 的 first_project.dllsecond_project.dll 中,

任何人都可以帮我如何正确使用项目间共享资源?

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. Every Form in this project can access this file from the designer. But I can see in the designer ImageResource.resx file to use it from second project (Reference to second project is present).

I have added the ImageResource.resx file as a link to my second project. 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. Also Visual Studio tells me that ImageResource.resx is present in two dll's first_project.dll and second_project.dll

Can anybody help me with How to correctly use shared resources between projects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

潇烟暮雨 2024-10-05 06:54:26
  1. 共享资源的正确方式是创建全局共享项目。创建一个名为Resources的新项目:

Solution Explorer,显示示例解决方案,其中包含一个项目命名为*Resources*

  1. 接下来我们必须向项目添加一些资源(例如图标)。像往常一样执行此操作。转到项目设置,选择选项卡资源添加现有文件...到项目。我们可以看到图标被添加到项目中并被复制到本地文件夹:

例如,Resources 项目的 Resources 选项卡显示小狗的图片,解决方案资源管理器显示资源项目的资源文件夹包含小狗的图片

  1. 下一步包括将此图标添加到其他项目。请注意重要的区别,您需要将此图标添加为链接

    添加为链接可以避免资源重复。在同一解决方案中创建一个新项目并将其命名为Main。在这个新项目中创建一些文件夹,将其命名为“Resources”(我们目的的逻辑名称)。然后右键单击该文件夹,选择添加现有项目...,然后从共享项目文件夹中选择图像文件。请务必在此处使用添加为链接!如果操作正确,新添加的文件的图标看起来会略有不同(见下文):

在 *添加现有项目* 对话框中,从“添加”按钮的下拉列表中选择“添加为链接”选项

添加的资源图标必须如下所示

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

  1. 现在我们必须设置构建操作 将此文件设置为。为此,选择文件并转到“属性”窗口。对于构建操作选择。我们需要这样做以避免将此图标嵌入到程序集中:
  1. 最后我们需要将链接的文件添加到相应项目的资源中。打开我们刚刚添加文件的项目的项目属性。选择资源选项卡并将链接的文件拖到那里:

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

这些是在项目之间共享图标必须执行的五个简单步骤。你可能会问“这样做有什么好处?”好处是:

  • 我们将资源存储在一个地方,并且
  • 很容易用新图标替换图标。
  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.
路还长,别太狂 2024-10-05 06:54:26

这对我不起作用,我找到了另一种(VS2015+)方法。
参见https://stackoverflow.com/a/45471284/4151626

总之,共享项目直接包含到外设中项目。因此,即使 IDE 不支持共享项目中的 元素。可以通过文本编辑器将 元素添加到共享项目中。然后在构建过程中将它们合并到外围项目中。

(对超链接表示歉意。为了清楚起见,我只是重新发布答案,但 stackoverflow 编辑器对此进行了严厉打击,删除了重复的答案,以免您受到 ??? 的困扰。)

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

合约呢 2024-10-05 06:54:26

您可以使用符号链接将文件共享到多个文件夹吗?

视窗:

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.

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.
记忆で 2024-10-05 06:54:26

如果资源文件确实在项目之间共享,则应该将其放在共享项目中。

Solution 'Sample'
   Project Sample.Data
   Project Sample.Business
   Project Sample.UI
   Project Sample.Resource   //shared resources are put in a shared project  

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  
夏九 2024-10-05 06:54:26

如果该资源不是公开,则您无法看到该资源,并且默认设置为“好友”。您可以在可视化设计器(右上角)中设置“访问修饰符”。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文