The Resource Application Block QuickStart Sample Applications

The two samples that are supplied with the Resource Application Block are contrived to demonstrate all of the features of the Resource Application Block.

Note: before trying out the samples please check the configuration files with the Enterprise Library Configuration Console. The samples use file based providers with absolute path names. These files are quite likely to be in a different place on your computer as they were on mine.

The Windows Forms based Message of the Day Application

Message of the Day Form - English Message of the Day Form - French Message of the Day Form - German

The key features demonstrated here are the standard mechanism for getting at resources, the strongly typed class method of getting at resources and the integration with the Windows Forms designer reflection-based component property resource management feature,

The application uses two resource managers, one for managing the messages of the day and another for managing the form resources:

[C#]
// Resource Manager holding the message resources. This is the default resource manager
// in configuration, hence no need to give the instance name.  This is also a Binary resource
// which means that the resources come directly from the "*.resources" files.
private ResourceManager messageResources = ResourceFactory.GetResourceManager();
// Resource Manager holding the form resources.  Because this is not the default resource manager
// you need to include the instance name. This is also an Xml resource which means that the 
// resources come directly from the "*.resx" file which can be edited independantly from the
// application.
private ResourceManager formResources = ResourceFactory.GetResourceManager("Form Resource Manager");


Also a strongly typed class was generated for the form resources and used in the MessageOfTheDay() method. This method also uses the standard mechanism for getting at resources.

[C#]
/// <summary>
/// Set the message of the day.
/// </summary>
/// <remarks>
/// The message of the day label is fetched from form resources using a strongly typed class,
/// while the message of the day text is fetched from message resources using an indexer.
/// </remarks>
public void MessageOfTheDay()
{
	// Message of the day label. Note the use of the generated strongly typed class
	// this class was generated from the Enterprise Library configuration tool
	string messageLabel = FormResources.labelMessageOfTheDay_Text;
	this.labelMessageOfTheDay.Text = messageLabel + ( (DayName)this.comboDays.SelectedItem ).NameValue;

	// Message of the day text. Here we use string manipulation to calculate the resource key
	// and then apply the key to the resource manager default indexer for string resources.
	string message = messageResources[( (DayName)this.comboDays.SelectedItem ).NameKey + "Message"];
	this.textMessage.Text = message;
}


Finally you see the reflection-based component property resource management feature in the ApplyResources() method

[C#]
/// <summary>
/// Reinitialize the form components that take localised information.
/// </summary>
/// <remarks>Note how we demonstrate the use of the ApplyResources method from the resource manager.
/// This method uses reflection to match resources found in the resource file with the properties of
/// a given form component.  For clarity I have used the exact same component key names as would
/// be generated by the Windows Form designer.  Have a look at the FormResources.resx file to see 
/// how these key names are formatted. Note how each property, except for the default property, takes
/// an initial double chevron.</remarks>
private void ApplyResources()
{
	this.SuspendLayout();
	formResources.ApplyResources(this, "$this");
	formResources.ApplyResources(this.comboCulture, "comboCulture");
	formResources.ApplyResources(this.comboDays, "comboDays");
	formResources.ApplyResources(this.labelMessageOfTheDay, "labelMessageOfTheDay");
	formResources.ApplyResources(this.pictureFlag, "pictureFlag");
	formResources.ApplyResources(this.textMessage, "textMessage");
	this.ResumeLayout(false);
}

The Web based Message of the Day Application

Message of the Day Web Page - English Message of the Day Web Page - French Message of the Day Web Page - German

The key features demonstrated here are the implicit mechanism for getting at local resources using the web resource provider and the explicit mechanism for getting at both local and global resources using the web resource provider,

Here is an example of the implicit use of resources through the web resource provider, the resource manager base name will be the name of the web page class, which in this case is default:

[ASPX]
<asp:Image ID="logo" runat="server" ImageUrl="~/App.ico" meta:resourcekey="logoResource1" />
&nbsp;
<asp:Label ID="title" runat="server" Text="title" meta:resourcekey="titleResource1" CssClass="title"></asp:Label>


Here is an example of the explicit use of resources through the web resource provider to get the national flag Url, using the syntax for getting at global resources, and implicit use of local resource to get the other pictureImage control properties:

[ASPX]
<asp:Image ID="pictureImage" runat="server" ImageUrl="<%$ Resources:default, nationalFlagUrl %>" meta:resourcekey="pictureImageResource1" />


The code-behind class uses the standard method and the strongly typed method for getting at resources in the MessageOfTheDay() method:

[C#]
/// <summary>
/// Set the message of the day.
/// </summary>
/// <remarks>
/// The message of the day label is fetched from form resources using a strongly typed class,
/// while the message of the day text is fetched from message resources using an indexer.
/// </remarks>
public void MessageOfTheDay()
{
	// Message of the day label. Note the use of the generated strongly typed class
	// this class was generated from the Enterprise Library configuration tool
	//string messageLabel = PageResources.labelMessageOfTheDay_Text;
	this.messageLabel.Text = PageResources.messageLabelResource1_Text + this.comboDays.SelectedItem.Text;

	// Message of the day text. Here we use string manipulation to calculate the resource key
	// and then apply the key to the resource manager default indexer for string resources.
	this.message.Text = messageResources[this.comboDays.SelectedValue + "Message"];
}

Last edited Dec 18, 2009 at 2:17 PM by ewdev, version 2

Comments

No comments yet.