Share via


Walkthrough: Loading Resources from a Satellite Assembly

This walkthrough shows how to load a resource string from a satellite assembly by using the Windows Presentation Foundation (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 Visual Studio Settings.

Prerequisites

You need the following components to complete this walkthrough:

  • Visual Studio 2008.

Creating the Project

The first step is to create the project for the application.

To create the project

  1. 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.

    Window1.xaml opens in the WPF Designer.

  2. From the Toolbox, drag a Button control onto the design surface.

  3. 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

  1. 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.

  2. Uncomment the following line.

    //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
    
    '<Assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)>
    
  3. Save the solution.

  4. In Solution Explorer, right-click the ResourceTest project and select Unload Project.

    Visual Studio unloads the ResourceTest project.

  5. 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.

  6. Insert the following XML at the same level as the other <PropertyGroup> elements.

    <PropertyGroup>
        <UICulture>en-US</UICulture>
    </PropertyGroup>
    
  7. Save and close the file.

  8. In Solution Explorer, right-click the ResourceTest project and select Reload Project.

  9. In Solution Explorer, change the name of the default RESX file from Resources.resx. to Resources.en-US.resx.

  10. 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

  1. In Solution Explorer, double-click Resources.en-US.resx.

    Resources.en-US.resx opens in the Resource Designer.

  2. Change the name of the default string from String1 to helloWorldString.

  3. Change the value of helloWorldString to Hello world.

  4. 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

  1. Open Window1.xaml.cs or Window1.xaml.vb in the Code Editor.

  2. .Insert the following code at the top of Window1.xaml.cs or window1.xaml.vb.

    using System.Reflection;
    using System.Resources;
    
    Imports System.Reflection
    Imports System.Resources
    
  3. 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"))
    
  4. Press F5 to build and run the application.

  5. Click the button to see the string value loaded from the satellite resource.

  6. Open the Debug/en-US folder to see the satellite assembly, which is named ResourceTest.resources.dll.

See Also

Reference

ResourceManager

Other Resources

Deployment and Localization using the WPF Designer

Encoding and Localization