Export (0) Print
Expand All

How to: Use a ResourceDictionary to Manage Localizable String Resources

This example shows how to use a ResourceDictionary to package localizable string resources for Windows Presentation Foundation (WPF) applications.

To use a ResourceDictionary to manage localizable string resources

  1. Create a ResourceDictionary that contains the strings you would like to localize. The following code shows an example.

    <ResourceDictionary 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:system="clr-namespace:System;assembly=mscorlib">
    
      <!-- String resource that can be localized -->
      <system:String x:Key="localizedMessage">en-US Message</system:String>
    
    </ResourceDictionary>
    

    This code defines a string resource, localizedMessage, of type String, from the System namespace in mscorlib.dll.

  2. Add the ResourceDictionary to your application, using the following code.

    <Application.Resources>
      <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="StringResources.xaml" />
        </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
    </Application.Resources>
    
  3. Use the string resource from markup, using Extensible Application Markup Language (XAML) like the following.

    <!-- Declarative use of string resource from StringResources.xaml resource dictionary -->
    <TextBox DockPanel.Dock="Top" Text="{StaticResource localizedMessage}" />
    
  4. Use the string resource from code-behind, using code like the following.

    // Programmatic use of string resource from StringResources.xaml resource dictionary 
    string localizedMessage = (string)Application.Current.FindResource("localizedMessage");
    MessageBox.Show(localizedMessage);
    
  5. Localize the application. For more information, see How to: Localize an Application.

Show:
© 2014 Microsoft