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!