Shared Resources XAML Intellisense
[ I'll provide a full solution download to anyone interested ]
Our Goals were:
1) Use images in XAML syntax like this:
<Image Name="imageSave" Height="16" Width="16" Source="ui:Images.ico_save_16.png"/>
2) Break on invalid image references at COMPILE - not at runtime.
3) Automatically add new when new images are copied into project.
4) Share resources within solution across projects.
5) Get intellisense for images from different projects in the same solution where possible (currently only in the codebehind - but the xaml editor will eventually provide intellisense here too).
6) VS 2008
_________________________________________________________
We created a separate "Resources" class library. In that class library we created an Images folder and under that an "ico" and "png" folders with all our images. Not added to "Resources" or "settings" as these generate only internal strongly typed - you need public.
Step 2:
Inside that class library create a text template - I called it Generator.tt. This text template will scan your directory of files and output something like this (below). We have two sub properties "ico" and "png" for the same resource... but you don't have to have that complexity.
// <auto-generated>
public static class Images
{
public static class ico_user_16
{
public static BitmapFrame png
{
get { return ResourceLocator.GetImageSource("ico_user_16.png"); }
}
}
//end ico_user_16
#endregion
------------------------------------------
The only really tricky part in creating the tt file was figuring out how to find the current directory to scan the project files...
string icoPath = this.Host.ResolvePath(@"Images\ico");
Now you can add references to that "Resources" class library from any project in your solution and get static resources in your xaml files... and share them across projects. As you add new images to your resource library simply save your Generator.tt file to re-generate your static resources.
Enjoy!!!
Comments
Anonymous
January 01, 2003
When are we going to get intellisense for StaticResources like images? This is causing me a lot of grief... I would be interested in any options, even Snippets like I get with XamlPad. I NEED ENCAPSULATION :(Anonymous
January 01, 2003
This is very close to what I've been looking for. I'd love to see the solution. You can email it to me at bdenham@topazti.com Thanks for this! Best, Bruce