Procedura: Usare un oggetto ResourceDictionary per gestire le risorse di tipo stringa localizzabiliHow to: Use a ResourceDictionary to Manage Localizable String Resources

In questo esempio viene illustrato come utilizzare un ResourceDictionary alle risorse di stringa localizzabile pacchetto per le applicazioni Windows Presentation Foundation (WPF).This example shows how to use a ResourceDictionary to package localizable string resources for Windows Presentation Foundation (WPF) applications.

Per usare un oggetto ResourceDictionary per gestire le risorse di tipo stringa localizzabiliTo use a ResourceDictionary to manage localizable string resources

  1. Creare un ResourceDictionary che contiene le stringhe che si desidera localizzare.Create a ResourceDictionary that contains the strings you would like to localize. Il codice seguente illustra un esempio.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>
    

    Questo codice definisce una risorsa di stringa, localizedMessage, di tipo String, dal System dello spazio dei nomi in mscorlib. dll.This code defines a string resource, localizedMessage, of type String, from the System namespace in mscorlib.dll.

  2. Aggiungere il ResourceDictionary per l'applicazione, tramite il codice seguente.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. Utilizzare la risorsa stringa dal markup, utilizzando Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML) simile alla seguente.Use the string resource from markup, using Extensible Application Markup Language (XAML)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. Usare la risorsa di tipo stringa dal code-behind, usando un codice analogo al seguente.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);
    
    ' Programmatic use of string resource from StringResources.xaml resource dictionary
    Dim localizedMessage As String = CStr(Application.Current.FindResource("localizedMessage"))
    MessageBox.Show(localizedMessage)
    
  5. Localizzare l'applicazione.Localize the application. Per ulteriori informazioni, vedere localizzazione di un'applicazione.For more information, see Localize an Application.