この例では、 ResourceDictionary を使用して、Windows Presentation Foundation (WPF) アプリケーションのローカライズ可能な文字列リソースをパッケージ化する方法を示します。
ResourceDictionary を使用してローカライズ可能な文字列リソースを管理するには
ローカライズする文字列を含む 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を定義します。次のコードを使用して、アプリケーションに ResourceDictionary を追加します。
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="StringResources.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>
次のような拡張アプリケーション マークアップ言語 (XAML) を使用して、マークアップの文字列リソースを使用します。
<!-- Declarative use of string resource from StringResources.xaml resource dictionary --> <TextBox DockPanel.Dock="Top" Text="{StaticResource localizedMessage}" />
次のようなコードを使用して、分離コードの文字列リソースを使用します。
// 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)
アプリケーションをローカライズします。 詳細については、「 アプリケーションのローカライズ」を参照してください。
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET Desktop feedback