![]() |
Platforms, Frameworks & Libraries »
Windows Presentation Foundation »
General
License: The Code Project Open License (CPOL)
StringsResourceGenerator, Custom Task + Addin for managing strings in ResourceDictionary from code.By jonnynolimitsCreates a xaml file for your strings and generates a class to semplify the using from code |
C# (C# 3.0), WPF
|
||||||||
|
Advanced Search |
|
|
|
||||||||||||||||
This Addin 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 core of the utility is the StringsResourceGeneratorTask, a custom task that runs BeforeBuild step of MsBuild.
You can use the task without StringsResourceGenerator addin, installing the dll in GAC and editing your project file manually.
At the bottom of project file, as children of Project node, you have to add:
<UsingTask TaskName="StringsResourceGeneratorTask" AssemblyName="StringsResourceGeneratorTask, Version=1.0.0.0, Culture=neutral, PublicKeyToken=157d1456ea169140" /> <Target Name="BeforeBuild"> <StringsResourceGeneratorTask Namespace="YourNamespace" AssemblyName="YourAssembly" ClassName="YourClassName" StringsResourceFileName="YourRelativeFileNamePath" /> </Target>
For UsingTask and Target tags see "References".
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.
The ClassName is the name of the class that will be generated.
The 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", any extension is necessary).
Reload the project. A dialog will appear:

Choose "Load project normally".
The task can generate code for cs only, but it's easy to expand it for other language.
You have to inherit from TypeCodeController class and override StringsCodeTemplate property and GeneratePropertyCode function.
The CSController class, used to generate cs 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 of the xaml file and have the same name of it with extension .xaml.cs.
The simpler way to obtain it is to insert a WPF UserControl.
In the xaml replace all with a empty ResourceDictionary with the reference to mscorlib library and prefix "system" for the namespace.
The prefix system is very important because it will be used by the custom task:
<ResourceDictionary xmlns="%22http://schemas.microsoft.com/winfx/2006/xaml/presentation%22xmlns:x=%22">http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="%22%22http://schemas.microsoft.com/winfx/2006/xaml%22%22">http://schemas.microsoft.com/winfx/2006/xaml" xmlns:system="clr-namespace:System;assembly=mscorlib"> </ResourceDictionary>
In the cs file remove all. If you want add a string, go to xaml file and add:
system:String x:Key="MyString">Hello World!</system:String>
And compile the project (see "Known Bugs"). Now if you want show a MessageBox you can write:
MessageBox.Show(YuourClassName.MyString);
In the Documentation.zip 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:

This Addin 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 xaml and cs files for managing strings from code.
Then you click the addin item. A dialog appears:

StringsResourceFileName rappresents the files name for 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 compile process.
ClassName rappresents the name of the class that will be generated.
Once you have generated the files you can modify (or delete) file name or class name,
but manually you have to edit the project file and change the attributes related to StringsResourceGeneratorTask.
The Addin uses a template for adding files to project. The template, during the installation, will go under Visual Studio 2008 CSharp item templates.
There isn't a template for vb.
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").
Support for vb projects.
After adding xaml and cs files with Addin menu item, the first compilation doesn't work. After the second one all works correctly.
If your project hasn't WindowsBase and PresentationFramework as references you have add them manually.
If you select the project root the addin 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 Custom Task:
http://community.bartdesmet.net/blogs/bart/archive/2008/02/15/the-custom-msbuild-task-ookbook.aspx
http://blogs.msdn.com/msbuild/archive/2005/11/23/496396.aspx
http://bartdesmet.net/blogs/bart/archive/2006/04/13/3896.aspx
For Addin Deploy:
http://www.codeproject.com/KB/install/AddinCustomAction.aspx
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 27 May 2008 Editor: |
Copyright 2008 by jonnynolimits Everything else Copyright © CodeProject, 1999-2009 Web11 | Advertise on the Code Project |