次の方法で共有


方法: ResourceDictionary を使用してローカライズ可能な文字列リソースを管理する

この例では、 ResourceDictionary を使用して、Windows Presentation Foundation (WPF) アプリケーションのローカライズ可能な文字列リソースをパッケージ化する方法を示します。

ResourceDictionary を使用してローカライズ可能な文字列リソースを管理するには

  1. ローカライズする文字列を含む ResourceDictionary という名前のプロジェクトのルートに、 ファイルを作成します。 次のコードは例を示します。

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

    このコードでは、mscorlib.dllのlocalizedMessage名前空間から、String型の文字列リソースSystemを定義します。

  2. 次のコードを使用して、アプリケーションに ResourceDictionary を追加します。

    <Application.Resources>
      <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="StringResources.xaml" />
        </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
    </Application.Resources>
    
  3. 次のような拡張アプリケーション マークアップ言語 (XAML) を使用して、マークアップの文字列リソースを使用します。

    <!-- Declarative use of string resource from StringResources.xaml resource dictionary -->
    <TextBox DockPanel.Dock="Top" Text="{StaticResource localizedMessage}" />
    
  4. 次のようなコードを使用して、分離コードの文字列リソースを使用します。

    // 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. アプリケーションをローカライズします。 詳細については、「 アプリケーションのローカライズ」を参照してください。