Click here to Skip to main content
13,456,687 members
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 27 May 2008

StringsResourceGenerator, Custom Task + Add-in for managing strings in ResourceDictionary from code

, 27 May 2008
Rate this:
Please Sign up or sign in to vote.
Creates a XAML file for your strings and generates a class to simplify use from code.


This add-in permits you to define strings into a ResourceDictionary of your project and use them in code just like you could do with RESX files. In this way, during localization process, you don't have to merge satellite DLLs.

The Custom Task for MSBuild

The core of the utility is StringsResourceGeneratorTask, a custom task that runs the BeforeBuild step of MSBuild. You can use the task without the StringsResourceGenerator add-in, installing the DLL in the GAC and editing your project file manually. At the bottom of the project file, as children of the Project node, you have to add:



        PublicKeyToken=157d1456ea169140" />





    StringsResourceFileName="YourRelativeFileNamePath" />

For information on the UsingTask and Target tags, see the "References" section. The StringsResourceGeneratorTask tag defines the custom tag, and its attributes, the properties. The Namespace attribute specifies the namespace of the class that will be created. The AssemblyName attribute defines the assembly of your project. ClassName is the name of the class that will be generated. StringsResourceFileName is the relative path of the files XAML and CS (for example, if the files are called LocalizableStrings.xaml and LocalizableStrings.xaml.cs under "Folder1", the value of StringsResourceFileName will be "Folder1/LocalizableStrings", extension is not necessary).

Reload the project. A dialog will appear:

Choose "Load project normally".

The task can generate code for C# only, but it's easy to expand it for other languages. You have to inherit from the TypeCodeController class and override the StringsCodeTemplate property and the GeneratePropertyCode function. The CSController class, used to generate the C# code, is an example.

In the project, you have to insert a .xaml file and a .cs file. The .cs file must be in the same path as the XAML file and have the same name, with the extension .xaml.cs.

The simpler way to obtain it is to insert a WPF UserControl. In the XAML, replace all with an empty ResourceDictionary with a reference to the MsCorlib library and a prefix "system" for the namespace. The prefix system is very important because it will be used by the custom task:





In the C# file, remove all. If you want to add a string, go to the XAML file and add:

system:String x:Key="MyString">Hello World!</system:String>

And, compile the project (see "Known Bugs"). Now, if you want to show a MessageBox, you can write:


In, there is a doc file "How to use StringsResourceGeneratorTask" that explains the settings of the task in a detailed way.

Here is the scheme for the task:

The Add-in

This add-in adds an item in the Tools main menu with the name "StringsResourceGenerator...". You must select an item or a folder in the project in which you want to add the XAML and cs files for managing the strings from code. Then, you click the add-in item. A dialog appears:

StringsResourceFileName represents the files name for the ResourceDictionary and code. For example, if you choose LocalizableStrings, two files will be added in the project: LocalizableStrings.xaml and LocalizableStrings.xaml.cs.

Namespace is the namespace in which the class will be added. AssemblyName is the name of the assembly generated by the compile process. ClassName represents the name of the class that will be generated. Once you have generated the files, you can modify (or delete) the file name or class name, but manually, you have to edit the project file and change the attributes related to StringsResourceGeneratorTask. The add-in uses a template for adding the files to the project. The template, during installation, will go under Visual Studio 2008 C# item templates. There isn't a template for VB.

Environments Supported

Visual Studio 2008 is the environment fully supported. If you use Visual Studio 2005, you can use the custom task only and set it manually (see "The Custom Task for MSBuild").

Future Development

Support for VB projects.

Known Bugs

After adding the XAML and cs files with the Add-in menu item, the first compilation doesn't work. After the second one, all works correctly. If your project doesn't have WindowsBase and PresentationFramework as references, you have to add them manually. If you select the project root, the add-in doesn't work! You have to select an item or a folder. If you modify or delete resource files previously created, the project file isn't updated automatically.


For the Custom Task:

For the Add-in Deploy:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Italy Italy
I am a biomedical engineer. I work in Genoa as software developer. I developed MFC ActiveX controls for industrial automation for 2 years and packages for Visual Studio 2005 for 1 year. Currently I'm working in .NET 3.5 in biomedical area.

You may also be interested in...

Comments and Discussions

GeneralBest Localization Plug-in for Visual Studio. Pin
Alexander Nesterenko17-Dec-08 21:47
memberAlexander Nesterenko17-Dec-08 21:47 
GeneralVB support Pin
klauswiesel12-Sep-08 0:47
memberklauswiesel12-Sep-08 0:47 
I have a principal question. As I am a beginner in both NET and WPF may be you can help?

OK, I use XAML-Resources for my UI, where I bind to using StaticResource/DynamicResource.
I like to use the same files in my business logic.

I looked into your code and saw what the addin really does.
So I wrote a small test function to verify it for me:

Dim sPath As String = ""
Dim oUri As Uri = Nothing
Dim sResult As String = ""

sPath = "Infor.Blending.Admin.Culture_de-DE;component/Dictionary_de-DE.xaml"
oUri = New Uri(sPath, UriKind.Relative)
Me.Resources = System.Windows.Application.LoadComponent(oUri)
sResult = Me.Resources("P.ModuleName")

Catch ex As Exception

End Try

This code runs fine, when started in a WPF Window.
When run in my business objects, Me (the class) has no Resources handle.
Is there an other / recommended way to use the same resources in non UI code other than using a handle to the main application (which then must be pushed in each business object)?

GeneralRe: VB support Pin
jonnynolimits15-Sep-08 3:15
memberjonnynolimits15-Sep-08 3:15 
GeneralVB support Pin
klauswiesel12-Sep-08 0:13
memberklauswiesel12-Sep-08 0:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180322.1 | Last Updated 28 May 2008
Article Copyright 2008 by jonnynolimits
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid