Walkthrough: Loading Resources from a Satellite Assembly
This walkthrough shows how to load a resource string from a satellite assembly by using the WPF Designer for Visual Studio. This facilitates localization for WPF applications.
In this walkthrough, you perform the following tasks:
Create the project.
Enable the satellite assembly.
Create and access a resource string.
When you are finished, you will have a simple application which retrieves a string from a satellite assembly.
Note
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Working with Settings.
Prerequisites
You need the following components to complete this walkthrough:
- Visual Studio 2010.
Creating the Project
The first step is to create the project for the application.
To create the project
Create a new WPF Application project in Visual Basic or Visual C# named ResourceTest. For more information, see How to: Create a New WPF Application Project.
MainWindow.xaml opens in the WPF Designer.
From the Toolbox, drag a Button control onto the design surface.
Double-click the button to create a Click event handler for the button control.
Enabling the Satellite Assembly
You enable the satellite assembly by making small changes to the AssemblyInfo and project files.
To enable the satellite assembly
Open AssemblyInfo.cs or AssemblyInfo.vb in the Code Editor. To see the file in Solution Explorer, you may have to click Show All Files.
Uncomment the following line.
//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
'<Assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)>
Save the solution.
In Solution Explorer, right-click the ResourceTest project and select Unload Project.
Visual Studio unloads the ResourceTest project.
In Solution Explorer, right-click the ResourceTest project and select Edit ResourceTest.csproj or Edit ResourceTest.vbproj.
The project file opens in the XML Editor.
Insert the following XML at the same level as the other <PropertyGroup> elements.
<PropertyGroup> <UICulture>en-US</UICulture> </PropertyGroup>
Save and close the file.
In Solution Explorer, right-click the ResourceTest project and select Reload Project.
In Solution Explorer, change the name of the default RESX file from Resources.resx. to Resources.en-US.resx.
In the Properties window, clear the resource file's Custom Tool value.
Creating a Resource String in the Satellite Assembly
Now you create a string in the resource file.
To create a resource string in the satellite assembly
In Solution Explorer, double-click Resources.en-US.resx.
Resources.en-US.resx opens in the Resource Designer.
Change the name of the default string from String1 to helloWorldString.
Change the value of helloWorldString to Hello world.
Save and close the Resource Editor.
Accessing the Resource String
Access the string in the satellite assembly by using the ResourceManager class.
To access the resource string
Open MainWindow.xaml.cs or MainWindow.xaml.vb in the Code Editor.
.Insert the following code at the top of MainWindow.xaml.cs or MainWindow.xaml.vb.
using System.Reflection; using System.Resources;
Imports System.Reflection Imports System.Resources
Insert the following code into the button1_Click event handler.
ResourceManager rm = new ResourceManager( "ResourceTest.Properties.Resources", Assembly.GetExecutingAssembly()); MessageBox.Show(rm.GetString("helloWorldString"));
Dim rm As New ResourceManager( _ "ResourceTest.Resources", _ Assembly.GetExecutingAssembly()) MessageBox.Show(rm.GetString("helloWorldString"))
Press F5 to build and run the application.
Click the button to see the string value loaded from the satellite resource.
Open the Debug/en-US folder to see the satellite assembly, which is named ResourceTest.resources.dll.