Nasıl yapılır: Yerelleştirilebilir Dize Kaynaklarını Yönetmek için ResourceDictionary Kullanma

Bu örnekte, yerelleştirilebilir dize ResourceDictionary kaynaklarını yerelleştirilebilir (WPF) uygulamaları için Windows Presentation Foundation için nasıl kullanacağız?

Yerelleştirilebilir dize kaynaklarını yönetmek için ResourceDictionary kullanmak için

  1. Yerelleştirmek ResourceDictionary istediğiniz dizeleri içeren bir oluşturun. Aşağıdaki kodda bir örnek gösterilmektedir.

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

    Bu kod, bir dize kaynağı localizedMessage olan , türünde bir dize kaynağını StringSystem mscorlib.dll.

  2. Aşağıdaki ResourceDictionary kodu kullanarak uygulamasını ekleyin.

    <Application.Resources>
      <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="StringResources.xaml" />
        </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
    </Application.Resources>
    
  3. Biçimlendirmeden dize kaynağını aşağıdaki gibi Extensible Application Markup Language (XAML) kullanarak kullanın.

    <!-- Declarative use of string resource from StringResources.xaml resource dictionary -->
    <TextBox DockPanel.Dock="Top" Text="{StaticResource localizedMessage}" />
    
  4. Aşağıdakine benzer bir kod kullanarak arka koddan dize kaynağını kullanın.

    // 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. Uygulamayı yerelleştirin. Daha fazla bilgi için bkz. Bir Uygulamayı Yerelleştirme.