views:

62

answers:

1

I have some resources (images in this case) in a resource file that I use on controls in my Windows Forms project. The Visual Studio Resource Selection dialog doesn't have very good support for choosing images from resource files unless they're in specific locations, but you can edit the designer file directly, and this works just fine; the application compiles and runs correctly, and the Windows Forms Designer is smart enough to not mess up my hand-edited code.

// in an assembly named ResourceConsumer
this.button1.Image = global::ResourceConsumer.Properties.Resources.Close32x32;

Now I want to move those resources to an external assembly so they can be used by multiple applications. I can set up an assembly to expose its resources without a problem (as long as I'm using Visual Studio 2008 or later), and this works fine. When I change the designer code to reference the image from its new location, the code compiles and runs correctly, but now the Windows Forms Designer changes my code whenever it generates code; it embeds the binary of the image in the local resource file and references it from there.

// ResourceProducer is an external assembly containing resources
this.button1.Image = global::ResourceProducer.Properties.Resources.Exit32x32;

is changed by the Windows Forms Designer to:

this.button1.Image = ((System.Drawing.Image)(resources.GetObject("button1.Image")));

The Windows Forms Designer seems to understand pulling a resource from within the same assembly, but not an external one. Is there any way to have the Windows Forms Designer allow me to use a resource from an external assembly?

+1  A: 

Nope, the designer doesn't support this. Important that it works the way it does, localization through satellite assemblies wouldn't work otherwise.

You can do this but you have to write the code yourself. Pretty much what you find in the Resources.Designer.cs file. Do consider if this is worth the effort, it isn't very maintainable and sharing resource assemblies isn't much of an optimization. A terabyte disk is less than a hundred bucks.

Btw: never edit the Resources.Designer.cs file yourself.

Hans Passant
Indeed, I have not been able to find anyway to make the designer support this, but what do you mean when you say "localization through satellite assemblies wouldn't work?" We would like to consolidate resources to reduce code duplication and localization effort; it would be easier to include and localize a resource once rather than in every project that needs that it. Wouldn't the localized resources be retrieved based on the resource assembly's satellite assemblies in this case? If so, we could still use a resource assembly for projects that don't need designer support.
CodeSavvyGeek