|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWith the release of .NET 2.0, Microsoft provides a type-safe (code-behind) wrapper class that wraps resource files. This is a huge improvement over the old error-prone way of accessing resources. We can now access resources like images, icons, files and strings just by using the resource’s identifier as a property of this wrapper class. For example, suppose we have an image file called MyCompanyLogo.png and a resource file called Images.resx. The resource file includes the image as follows: name = “MyCompanyLogo” file = “.\MyCompanyLogo.png”
The code-behind file creates a class called internal Image MyCompanyLogo { get; }
Now in our source code, we can access the image file MyCompanyLogo.png simply by accessing the type-safe property: Image pic = Images.MyCompanyLogo;
The ProblemAs it turns out, there is still one major problem area; runtime errors are possible when using formatted strings. A formatted string is a Console.WriteLine(string.Format(“Welcome {0}”, name));
At this point, we want to make the However, we now have a problem. Because the formatted Console.WriteLine(LocalizedStrings.WelcomeMessage);
And the output looks like this: Welcome {0}
Clearly that isn't what we want and thus we have a runtime bug. The correct usage is: Console.WriteLine(string.Format(LocalizedStrings.WelcomeMessage, name));
Another problem with this approach occurs if we decide to add more parameterized content to the Also, another runtime bug occurs because we now have two replaceable parameters instead of one. We have to remember to search our code and fix every place we are using this The SolutionThe solution is to provide a smarter layer over the existing structure provided by Visual Studio. Our new layer produces a code-behind file similar to that created by Visual Studio. Non-string resources (images, icons, etc.) and To access a non-formatted Console.WriteLine(LocalizedStrings.MyNonFormattedString);
To access a Console.WriteLine(LocalizedStrings.WelcomeMessage(firstName, lastName));
In the above case, the Welcome {0} {1}
In cases where access to the original string rawMessage = LocalizedStrings.Raw.WelcomeMessage;
UsageUsing
Adding Replacement Parameter InformationReplacement parameters are used to build the method parameters for a formatted Replacement Parameter Information SyntaxThe syntax of the replacement parameter information is very simple yet flexible. It consists of the following rules:
To exclude a ExamplesHere is a typical string resource editor view:
Note: Comments are terminated by carriage returns ( Here is part of the code-behind file generated from the example resources: public string InvalidIdentifier(string className) {…}
public string Hi(string name, int age) {…}
public string UnformatedMessage { get {…} }
/// <param name = “address”>Can't accept PO boxes</param>
/// <param name = “zip”>
/// The zip must include the 4 digit extension. I.e. 43249-1234
/// </param>
public string Address(string address, string city, string state, string zip) { … }
The Enhanced Resource File Code Generator source and executable can be downloaded from the links at the top of this article. Enjoy! History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||