I have five projects in my solution. Each开发者_C百科 of them uses some icons (many of these icons are the same in the different projects) and I want to store them in one place.
I tried to use separate project for storing icons, but I got fiasco..
When I use icon from other project (project which contains all icons) Visual Studio automatically copied this icons to the form.resx
file.
Explanation:
By the first I can't attach icon (from other project) to my button
from designer
. Therefore I must to go to the form.designer.cs
and manually attach the icon to the button. After these operations VS rebuilt the auto-generated part in form.designer.cs
replaced my code with own and copied the icon to the form.resx
file.
//my code
this.btnCompile.Image = SharedProject.Properties.Resources.compile;
//code replaced with VS
this.btnCompile.Image = ( (System.Drawing.Image)( resources.GetObject( "compile.Image" ) ) );
You can see that image was copied to local project to the resources. In this case to change button's image is insufficient to replace only my image (compilation.png) in shared project. I need to replace also it in each form where it used.
Question: How to manage icons in solution to avoid duplication and this big inconvenience?
I've solved exactly the same problem but with one limitation. Let's see two possibilities:
1) You want to have ability to update all images by overriding one folder with images in app folder.
You can use Service as @Farzin Zaker suggested. The drawback of that solution is you have to dynamically set up images to controls and in design mode you will not see control with correct images.
2) You want to use Designer.
In that case you have to add those images as resources. The trick is to make VS use relative path to images from the specified folder. By default VS copies all resource files to project Resources folder. But if VS sees that in Resources folder "registered" other file with the same name VS stores in .resx relative path to image. So If you want to add the same image in two project you have to:
- Create folder Resources
- Add image as existing item (as link): in VS you will see the link to image, set action to None
- Add image to project resources section.
You can design a Service project witch will serve Icons or other resources for your other projects. you should just implement an interface like this in your application :
public interface IResourceProvider
{
Image RetrieveIcon(string key);
//you can add other resource types here.
}
then implement this interface in a new class and return related Icon to each key passed to its methods. here you need to browse resx file of this project for requested Icon (Resource)
then in you application you should just call your ResourceProvider class to priovide required resource Items.
精彩评论