如何在 C# 解决方案中跨多个项目共享资源? [英] How can I share resources across multiple projects within a C# solution?

查看:88
本文介绍了如何在 C# 解决方案中跨多个项目共享资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个解决方案中有多个项目,我希望它们共享一个图形资源池.我试过这个:

I have multiple projects in a solution and I'd like them all to share one pool of graphics resources. I've tried this:

1 - 创建项目 1,将其资源文件设为公开,并为其添加一些图形.

1 - Created project1, made its resource file public, added some graphics to it.

2 - 创建 project2,Alt+将 Resources.resx 从 project1\Properties 拖到 project2(不在 Properties 文件夹中)

2 - Created project2, Alt+dragged Resources.resx from project1\Properties to project2 (not in the Properties folder)

3 - 将 project2 中的引用添加到 project1

3 - Add reference in project2 to project1

所以,现在 project1 中的所有图像都可以在 project2 中使用了.到现在为止还挺好.我可以在设计时很好地使用它们.

So, now all the images from project1 are available in project2. So far, so good. I can use them at design time just fine.

但是,当我想在运行时访问它们时,我会尝试这个(在 project2 中)...

But, when I want to access them at runtime, I try this (in project2)...

Image img = project1.Properties.Resources.image14;

这会因 MissingManifestResourceException 而崩溃.

And that crashes with a MissingManifestResourceException.

我在这里做错了什么?有没有更好的方法可以解决这个问题?我想要做的就是将所有图形保存在一个地方,因此如果我添加资源,它就可用于解决方案中的所有项目.

What am I doing wrong here? Is there a better way I could approach this? All I'm trying to do is maintain all my graphics in one place, so if I add a resource, it becomes available to all projects in the solution.

推荐答案

刚刚按照以下步骤构建了一个示例:

Just built an example following these steps:

  1. 创建一个类库来保存资源(项目 1)
  2. 创建使用者项目(项目 2)
  3. 在Project 1中添加一个资源文件(GlobalResources.resx)并添加一个资源项Information
  4. 将资源文件的BuildAction改为Embedded Resource
  5. 将资源文件的请勿复制更改为
  6. 检查资源文件的自定义工具是否设置为PublicResXFileCodeGenerator
  7. 将类库(项目 1)的引用添加到使用者项目(项目 2).
  8. 在您想要使用的任何位置添加资源命名空间引用.
  9. 终于开始工作了:GlobalResources.Information
  1. Create a class library do hold the resources (Project 1)
  2. Create the consumer project (Project 2)
  3. Add a resource file (GlobalResources.resx) in the Project 1 and add a resource item Information
  4. Change the BuildAction of the resource file to Embedded Resource
  5. Change the Do not copy of the resource file to False
  6. Check if the Custom Tool of the resource file is set to PublicResXFileCodeGenerator
  7. Add a reference to the class library (Project 1) to the consumer project (Project2).
  8. Add the resource namespace reference wherever you want to use it.
  9. Finally it is working: GlobalResources.Information

应该很简单.

您担心在设计时属性编辑器中使用外部资源文件.很抱歉地通知,对此没有标准支持:(

You are concerned about using an external resource file inside the design time property editor. Sorry to inform that there is no standard support for this :(

但是,如果您认为收益大于努力:

However, if you think that the benefits are greater than the effort:

Visual 中跨项目共享 WinForms 资源的问题工作室

如何如何让 Windows 窗体设计器使用来自外部程序集的资源?

希望有帮助.

这篇关于如何在 C# 解决方案中跨多个项目共享资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆