Visual Studio 中跨项目共享 WinForms 资源的问题
我已将大型多项目解决方案中的所有图像资源移动到专门为共享资源创建的新项目。
添加对使用共享图像的其他项目的新引用正在按预期工作。 下面所选按钮中的图像继续出现在设计器中,但存在问题。
在 VS Designer 中,我无法在图像编辑器中选择共享资源,而必须手动编辑设计器代码。
更具体地说,在“图像编辑器”对话框(从现有控件的“图像”属性访问)中,我选择“从文件/资源中选择...”以弹出“选择资源”对话框(如图所示)。在那里,我曾经访问所有本地项目资源 (Resources.resx),我现在想添加对新共享资源项目的访问权限,理想情况下,我会在显示的下拉列表中添加第二个项目,该项目会切换到共享资源项目的 resx
。有更好的方法吗
? 1NzxO.png" alt="替代文字">
I've moved all image resources in a large multi-project solution to a new project specifically created for shared resources.
Adding a new reference to other projects where the shared images are used is working as expected. The image in the selected button below continues to appear in designer but with issues.
In VS Designer, I'm unable to select shared resources in the Image Editor, having instead to manually edit the designer code.
More specifically, in the Image Editor dialog (accessed from the 'Image' property of an existing control) I select 'Select from File/Resource..." to pop up the Select Resource dialog (shown). There, where I used to access all local project resources (Resources.resx), I would like now to add access to the new shared resources project. Ideally, I would add a second item to the dropdown shown which switches to the resx for the shared resources project.
How can this be done? Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我能够在项目之间共享 resx 文件并在 VS 2008 SP1 的设计器中使用它(我不知道其他版本)。 与其他方法不同,这里资源是静态和动态共享的,因此在运行时实际上存在共享资源的一份副本。 没有存储或内存浪费,并且可以轻松维护大量项目。
按照指南进行操作并告诉我它是否适合您:
解决方案资源管理器中的项目应如下所示:
现在,在任何项目中,您都需要将 resx 文件共享执行此操作:
将“资源”项目添加为依赖项;
手动编辑项目文件(*.csproj)并在资源文件 ItemGroup 中添加以下行(如果要查看,请创建本地资源文件):
显然,“Include”属性应该是 GlobalResources 的正确相对路径。 RESX。
在我的项目中,执行此操作时,会生成以下行并自动添加到项目中:
如果未添加,请手动添加。
现在,该项目在解决方案资源管理器中应如下所示(类似文件应以不同方式标记):
最后两个步骤:
您就完成了:此时您应该真正能够使用您在共享中添加的资源resx 在设计器中选择问题的“项目资源文件”对话框中的“GlobalResources.resx”。 正如所说,即使在运行时它也确实是共享的! 在“属性”面板中,您应该看到外部项目的完整命名空间和类。 如果您删除对“资源”项目的依赖关系,它甚至无法编译。 如果它对你不起作用,坚持直到它起作用,以防万一告诉我指南哪里错了。 它必须起作用! 让我知道。
I was able to share a resx file between project and using it in the designer in VS 2008 SP1 (I don't know other versions). Differently from other approaches, here resources are statically and dynamically shared, so at runtime there's really one copy of the shared resources. No storage or memory waste and it's easy to maintain with tons of projects.
Follow the guide and tell me if it works for you:
The project in the Solution Explorer should look like this:
Now in any project you need the resx file to be shared do this:
Add the "Resource" project as a dipendency;
Edit manually the project file (*.csproj) and add the following lines in the resources file ItemGroup (create a local resource file if you want to see it):
Obviously, the "Include" attribute should be the correct relative path to GlobalResources.resx.
In my project, the following lines are generated and added to the project automatically when I do this:
If they aren't added, add them manually.
Now the project should look like this in the Solution Explorer (likend files should be marked differently):
Last two steps:
And you are done: at this point you should really be able to use the resources you added in the shared resx in the designer selecting "GlobalResources.resx" in the "Project Resource file" dialog of your question. And as told it's really shared even at runtime! In the Properties panel you should see the full Namespace and class of the external project. If you remove the dependency on the "Resource" project it doesn't even compile. If it doesn't work for you, insist until it works and in case tell me where the guide is wrong. It have to work! Let me know.
我最近遇到了同样的问题。 查看了 MS 文档但找不到任何好的解决方案。 我最终能够使用下面描述的方法让 WinForms 设计器中的图像编辑器识别来自其他项目的资源文件。 这是 hacky - 如果有人有更干净的解决方案想要分享,请发布。 我只在 VS 2008 上测试过。
在开始之前,我应该说,如果您只想使 Resources.resx 文件中的项目可供其他项目访问,您只需编辑其属性并将生成的类的访问修饰符设置为 public,以便它使用 PublicResXFileCodeGenerator。 仅当您想让 WinForms 设计者按照 Douglas 的上述帖子中所述拾取此资源文件中的图像时,才需要执行以下操作。
假设您的解决方案具有以下布局。 各个项目的默认命名空间(在解决方案资源管理器中右键单击项目节点 -> 属性 -> 应用程序选项卡)在这里很重要,因此我将其放在项目名称后面的括号中。
您想要在 SharedProject 中创建一个资源文件,其中包含可供 WinFormsProject1 和 WinFormsProject2 使用的图像。
首先在SharedProject的根目录中创建一个新的资源文件,我们将其命名为SharedResources.resx。 重要的是,该文件使用 ResXFileCodeGenerator 而不是 PublicResXFileCodeGenerator,原因稍后会清楚。
在 Visual Studio 中编辑 SharedResources.resx 并添加要共享的图像资源。 Visual Studio 还应在 SharedProject 中为您生成一个名为 SharedResources.designer.cs 的类文件。
您的解决方案现在应如下所示:
如果使用文本编辑器打开 SharedProject.csproj,您应该会看到以下条目:
现在您需要使 WinFormsProject1 内的资源可访问。 为此,您需要在文本编辑器中编辑 WinFormsProject1.csproj,因为您需要设置的某些属性未在 Visual Studio 中公开。
打开 WinFormsProject1.csproj 文件并在 ItemGroup 中添加以下内容:
这里的关键内容是 CustomToolNamespace 和 LogicalName。 默认情况下,Visual Studio 使用项目的默认命名空间来命名资源并生成 *.Designer.cs 文件。 由于 SharedProject 和 WinFormsProject1 具有不同的默认命名空间,因此默认行为会导致 WinFormsProject1 中嵌入的资源与 *.Designer.cs 不兼容SharedProject 内的文件。 可以通过覆盖 CustomToolNamespace 和 LogicalName 来防止这种情况。
现在对 WinFormsProject2.csproj 执行相同的操作。 返回到 Visual Studio。 它会注意到您已更改 csproj 文件并要求重新加载项目 - 选择“重新加载”。 您应该发现,在 WinFormsProject1 和 WinFormsProject2 中设计表单时,现在可以从 SharedResources.resx 中选择图像。
本质上,这一切的作用是使 VS 在 WinFormsProject1 和 WinFormsProject2 中包含来自 SharedProject 的 SharedResources.resx 文件项目。 只有一个 .resx 源文件(在 SharedProject 中),但是当您编译解决方案时,实际上会存在三个相同的类,称为 SharedProject.SharedResources,每个类中都有一个三个程序集,但由于所有程序集都具有内部可见性,即使您在项目之间添加引用,它们也不会相互干扰。
I came accross the same problem recently. Looked around MS documentation but couldn't find any good solutions. I was eventually able to get the image editor in the WinForms designer to recognize resource files from other projects using the method described below. It is hacky - if anyone has a cleaner solution they want to share please post. I have tested this on VS 2008 only.
Before I start I should say that if you just want to make the items in a Resources.resx file accessible to other projects, you can just edit its properties and set the access modifier for the generated class to public so it uses PublicResXFileCodeGenerator. The gymnastics below are only needed if you want to get the WinForms designers to pick up images inside this resource file as described in the post above from Douglas.
Say that your solution has the following layout. The default namespace for the various projects (right click on a project node in solution explorer -> Properties -> Application tab) is important here, so I've put this in brackets after the project names.
You want to create a resource file in SharedProject containing images that can be used by WinFormsProject1 and WinFormsProject2.
First create a new Resource file in the root of SharedProject, let's call it SharedResources.resx. It is important that this file uses ResXFileCodeGenerator rather than PublicResXFileCodeGenerator for reasons which will become clear later.
Edit SharedResources.resx in Visual Studio and add the image resources you want to share. Visual Studio should also generate a class file for you inside SharedProject called SharedResources.designer.cs.
Your solution should now look like this:
If you open up SharedProject.csproj with a text editor you should see the following entries:
Now you need to make the resources accessible inside WinFormsProject1. To do this you'll need edit WinFormsProject1.csproj in a text editor because some of the properties you need to set are not exposed inside Visual Studio.
Open up the WinFormsProject1.csproj file and add the following inside an ItemGroup:
The key things here are CustomToolNamespace and LogicalName. By default Visual Studio uses the project's default namespace to name resources and to generate the *.Designer.cs file. Since SharedProject and WinFormsProject1 have different default namespaces the default behaviour causes the resource that is embedded in WinFormsProject1 to be incompatible with the *.Designer.cs file that is inside SharedProject. This can be prevented by overriding CustomToolNamespace and LogicalName.
Now do the same for WinFormsProject2.csproj. Go back to Visual Studio. It will notice that you've changed the csproj files and ask to reload the projects - choose "reload". You should find that you can now choose images from SharedResources.resx when designing forms in both WinFormsProject1 and WinFormsProject2.
Essentially what all of this does is make VS include the SharedResources.resx file from SharedProject in the WinFormsProject1 and WinFormsProject2 projects. There is only one .resx source file (in SharedProject), but when you compile the solution there will actually be three identical classes called SharedProject.SharedResources, one in each of the three assemblies, but since all have internal visibility they won't interfere with one another even if you add a reference between the projects.
在 VS2012 中的项目之间共享资源
仅使用设计器即可通过 3 个简单步骤
!
SHARING RESOURCES BETWEEN PROJECTS IN VS2012
in 3 easy steps using the designer only!
VOILA!
对于 Visual Studio 2015+
有一种简单、直接的方法,但 VisualStudio UI 不支持该方法。 因此,如果您不害怕编辑项目文件,请继续阅读。
如果您尚未这样做,请将新的共享项目添加到解决方案中。
如果您尚未执行此操作,请将新共享项目作为对外围项目的共享项目引用包含在内。< a href="https://i.sstatic.net/kh6tq.png" rel="nofollow noreferrer">
注意这个共享项目是如何以直接的方式包含在外围项目(
.csproj
文件)中的:因此,即使 VS UI 没有公开它,所有内容都在共享项目(
.projitems
文件)直接包含在外围项目中。 将以下资源定义添加到共享项目(.projitems
文件)中会将它们直接包含到您的外围项目中。 但是,由于 VS UI 不支持共享项目的Resource
元素,因此这些文件将在 IDE 中隐藏并忽略。最后,虽然您的外围项目资源可能具有相对路径(例如
".\Resources\main.ico"
),但这些资源会使用基本 ID(而不是绝对或相对)编译到您的外围项目中。 例如:For Visual Studio 2015+
There is an easy, straightforward way, that is not supported in the VisualStudio UI. So if you are not afraid of editing the project files, read on.
If you have not already done so, add a new shared project into the solution.
If you have not already done so, include the new shared project as a shared project reference to your peripheral project.
Note how this shared project is included in the peripheral project (
.csproj
file) in a straight forward manner:Thus, even though the VS UI does not expose it, everything in the shared project (
.projitems
file) is included directly into the peripheral project. Adding the following resource definitions to the shared project (.projitems
file) will include them directly into your peripheral project. But, because theResource
element is not supported by the VS UI for shared projects, these files will be hidden and ignored in the IDE.Lastly, whereas your peripheral project resources might have relative paths (e.g.
".\Resources\main.ico"
), these resources are compiled into your peripheral project with base ids, not absolute or relative. e.g.:您只需添加一个带有 RESX 文件的新类项目,其中包含资源(图像、文本等)。 在其他项目中添加对此的引用并使用资源类名称来访问所需的资源,例如:Project.SharedImages.Upload_64x64(结果是在本例中可以使用的位图)
You only need add a new class project with a RESX file where you include the resources (images, texts, etc). After in the other projects add a reference to this and use the Resource class name in order to access the resource needed, for example: Project.SharedImages.Upload_64x64 (the result is a Bitmap ready to use in this case)
查理的回答效果很好!
只需一个小更改:如果您想跳过最后两个步骤,请在修改项目文件时使用此版本的包含项目。
Charlie's answer works great!
Just a small change: If you want to skip the last two steps, use this version of the included items when you modify a project file.
对于 SDK 样式项目,请使用以下方法;对于非 SDK 样式项目,请复制 .targets 文件(未经测试)的内容。
1:在您的解决方案旁边创建以下文件。 将占位符替换为全局资源的正确信息。 例如将其命名为“IncludeGlobalResources.targets”
2:转到要使用全局资源的项目文件(我有 SDK 样式项目)。 引用创建的文件:
3:现在您只需将 2. 添加到您想要使用全局资源的每个项目即可。 您还可以轻松地使用其他全局资源扩展该文件。
Use the following approach for SDK Style projects or copy the contents of the .targets file (not tested) for non-SDK Style projects.
1: Create the following file next to your solution. Replace the placeholders with the proper information of your global resource. Name it for example "IncludeGlobalResources.targets"
2: Go to the Project-File (I had SDK-Style Projects) where you want to use the global resource. Reference the created file:
3: Now you can just add 2. to every project you want to use the global resource. Also you can extend the file with other global resources easily.