Strongly typed resource model in Whidbey
In Visual Studio 2003 and earlier, managing resources like images and icons in your project is somewhat difficult. For example, when you try to assign an image to a PictureBox through the property grid, you get the FileOpenDialog. When you pick an image file, the image is dumped into the form's resx file as a binary blob. If you want to use the same image in a different form (lets say all your forms have the same background image), you would need to pick the same image over and over again, thereby duplicating the binary blobs in each form's resx file. Now, what if you needed to edit the image, say, to insert a company logo? Not very easy. What if you decided to pick a new image? You need to do so all over again for all your forms. Also, the code to access the resources is weakly typed, i.e., something like:
this.BackgroundImage = (System.Drawing.Image) resources.GetObject(”MyImage.jpg”);
where 'resources' is of type ResourceManager.
One other thing: the default editor for resx files is a table of resource names. It isn't very easy to add new resources or edit existing ones.
Ofcourse, you can work around these limitations through code, but this is the default design time experience.
In Whidbey, we spent quite sometime thinking about how we could improve this experience. The result was that we designed a new model to store and access resources in Visual Studio projects. Here are some of the highlights of the new model:
- When you try to assign an image to a control in the Whidbey designer, you will see a 'Select Resouce' dialog. This dialog will help you “import“ resources into project resource files. The imported resources, by default, will not get embedded into the resx files, but instead copied to a Resources folder in the project and referenced in the resx files by means of ResxFileRef.
- For each resx file, you can enable strongly typed access (whch typically will be automatically enabled for the default project resx file and any file you import resources into using the Select Resource dialog). This means that we will generate a class that allows you to access resources through strongly typed properties. For instance, the code in InitializeComponent will look something like:
Me.BackgroundImage = My.Resources.MyImage
in VB, and something similar in C# and J#.
- Since the entries in the resx files are file references rather than binary blobs, you can edit the images and the changes will automatically be picked up wherever those images are referenced. Since these resx files are project level rather than form level, you can access the resources across forms in your project.
- There is a new Managed Resource Editor in Whidbey that allows you to easily edit resx files - update entries, add entries, import resources, delete entries etc.
This should help improve the overall experience around managing project resources. Do let us know how you find this new model when you get a chance to play with it!
Comments
- Anonymous
May 24, 2004
The comment has been removed - Anonymous
May 24, 2004
Thanks Thomas. For storing application settings and configuration, you may find our new Client Settings feature useful. Here is my recent post about it: http://blogs.msdn.com/rprabhu/archive/2004/04/03/106996.aspx. - Anonymous
June 01, 2004
Please tell me that this works for image lists too!! (I'm still trying to get a hold of the TechEd version of Whidbey to play with it myself) - Anonymous
June 02, 2004
Chris: No, this model is not available for imagelists for the reasons below:
1) Imagelists use a special serialization scheme that serializes all the images in it into a single stream. We didn't want to change this behavior.
2) Imagelists already have good design time support through the image collection editor.
3) You already have typed access to the images in a Imagelist through its collection. - Anonymous
July 05, 2004
Will the Whidbey designer set a relative path or an absolute path for the "value" of the ResxFileRef (data element in the .resx file)? - Anonymous
July 07, 2004
Ly: The path will be relative to the path of the resx file. - Anonymous
January 02, 2006
How does a user control take adevantage of this "Managed Resource Editor" ?
If I'm writting a custom control that has a property that I want it in a resource (a text file for example) how do I instruct Visual Studio to show this "Managed Resource Editor" dialog when this property is edited ?
I've going through the code for PictureBox, Image and ImageEditor and could not find any reference to this editor/dialog. - Anonymous
January 18, 2006
Frank: Not sure if you are referring to the resource editor or the Select Resource dialog here, but I will assume the latter based on the context of your question.
Unfortunately, there is no way to reference the Select Resource dialog as a UITypeEditor for your custom property, since it is implemented in a VS package, and not in the framework. It will automatically show up for properties of type Image, and is designed to be used only for that type.