是否可以使用WinForms中的设计器从其他项目访问资源? [英] Is it possible to access resources from a different project using the designer in WinForms?

查看:47
本文介绍了是否可以使用WinForms中的设计器从其他项目访问资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当大的解决方案,其中包含许多项目.理想情况下,我想创建一个Resources项目,其中包含我在整个应用程序中所需的所有嵌入式图标.

I've got a rather large solution with many projects in it. Ideally I'd like to create a Resources project that contains all the embedded icons I need throughout my application.

我读过一些文章,人们在其中通过代码引用它们,但是可以在设计器中访问该项目的资源吗?换句话说,在设计器中单击表单的BackgroundImage属性时,我想选择资源.

I've read articles where people are referencing them through code, but is it possible to get access to this project's resources in the designer? In other words, I'd like to select the resources when clicking on the BackgroundImage property of a form in the designer.

如果没有,那么在尝试将资源与主项目脱钩时应该考虑哪种架构?每当我修改一两行代码时,释放一个100mb的可执行文件可能不是一个聪明的主意.

If not, what sort of architecture should I consider when trying to decouple my resources from my main project? It's probably not a smart idea to release a 100mb executable everytime I modify a line of code or two.

推荐答案

据我所知,您不能操纵设计器中其他程序集的资源.仅当前项目中的一个或多个.

From the best I can tell, you can NOT manipulate resources from other assemblies in the designer. Only the one(s) from the current project.

这是我的处理方式:

创建一个类库项目.在库中,添加|新商品|资源文件.将资源(图像,图标)添加到该文件(例如:Resource1.resx).在该库的一个类中,添加返回资源的公共成员.

Create a Class Library Project. In the Library, Add | New Item | Resources File. Add the Resources (images, icons) to that file (ex: Resource1.resx). In a Class in that Library add public members that return the resources.

public static class Class1
{
    public static Bitmap Image1 { get { return Resource1.Image1; } }
    public static Bitmap Image2 { get { return Resource1.Image2; } }
}

现在在您的项目中引用类库,现在可以在运行时以编程方式设置资源.

Now in your project reference the Class Library and now can programmatically set the resources at runtime.

public Form1()
{
    InitializeComponent();

    this.BackgroundImage = ClassLibrary1.Class1.Image1;
}

但是很遗憾,您不能使用设计器.:(但是,如果您不对其中的资源进行任何更改,则无需重新分发该dll.

But unfortunately you can’t use the designer. :( However, by doing this you will not need to redistribute that dll if you don't make any changes to the resources in it.

这篇关于是否可以使用WinForms中的设计器从其他项目访问资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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