如何在SharpDevelop 4.2中启动国际化的WPF项目?

Mar*_*ber 4 c# wpf xaml sharpdevelop internationalization

我想创建一个软件,用户可以在几种语言之间进行选择.

作为一个开始,我想学习如何处理国际化,因为我以前从未这样做过.

作为IDE我使用SharpDevelop或#develop,但你会拼写它.我想使用C#和WPF,因为我现在也在学习XAML/WPF.

所以我在ShardDevelop中创建了一个新的WPF项目.在主窗口上,我创建了一个ComboBox和一个TextBlock.

ComboBox获得两个条目:"德语"和"英语".textBlock应该显示"Hallo Welt!" 或"Hello World!",具体取决于所选的语言.

现在是我陷入困境的部分.我想每种语言都是XML/XAML-Style(有道理)的单独文件.这些文件在哪里以及如何加载它们及其内容以便加载所选语言的文本?

我发现了几个例子,但都是一些关于创建资源的DLL,并使用一些奇怪的程序进行反编译它们放回一个CSV文件......我不明白,是不是有一个更简单的方法?


我采取了下一步.现在通过"{StaticResource Strings.MainForm.hwText}"加载TextBlock的Text.它现在看起来像这样:

<TextBlock Text="{StaticResource Strings.MainForm.hwText}" />
Run Code Online (Sandbox Code Playgroud)

我还为德语创建了一个ResourceDictionary,为英语创建了一个ResourceDictionary,它们都定义了我在TextBlock中使用的键.

在Application.Resources Part i中,默认情况下加载一个ResourceDictionary.

现在的问题是:如何在运行时"卸载"这个词典并将其替换为另一个词典?

当然我使用ComboBox的SelectionChange-Event,但我在那里做什么?


问题解决了!!感谢kmatyaszek

虽然我根据我的需要改变了事件处理程序的代码:

Uri baseUri = new Uri(AppDomain.CurrentDomain.BaseDirectory);
Uri uri = new Uri(baseUri,"Languages\\lang."+((sender as ComboBox).SelectedItem as ComboBoxItem).Tag.ToString()+".xaml");
if(File.Exists(uri.LocalPath) || File.Exists((uri = new Uri(baseUri,"Languages\\lang.de-DE.xaml")).LocalPath)){
    ResourceDictionary dict = new ResourceDictionary();
    dict.Source = uri;
    this.Resources.MergedDictionaries.Add(dict);
}
Run Code Online (Sandbox Code Playgroud)

kma*_*zek 6

如果您创建了两个ResourceDictionary文件,则可以通过绑定DynamicResource.

例:

第一个资源文件(Lang.en-US.xaml):

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

    <system:String x:Key="Username">Username:</system:String>
    <system:String x:Key="Password">Password:</system:String>
    <system:String x:Key="close">Close</system:String>
    <system:String x:Key="login">Login</system:String>        
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

第二个资源文件(Lang.pl-PL.xaml):

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

    <system:String x:Key="Username">Login:</system:String>
    <system:String x:Key="Password">Has?o:</system:String>
    <system:String x:Key="close">Zamknij</system:String>
    <system:String x:Key="login">Zaloguj</system:String>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

在应用程序资源中设置默认语言

 <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Lang.en-US.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
 </Application.Resources>
Run Code Online (Sandbox Code Playgroud)

假设我们有如下的ComboBox:

<ComboBox Name="cbLang" Margin="2" SelectionChanged="cbLang_SelectionChanged" >
                <ComboBoxItem Content="English" Tag="en-US" />
                <ComboBoxItem Content="Polish" Tag="pl-PL" />
  </ComboBox>
Run Code Online (Sandbox Code Playgroud)

代码隐藏SelectionChanged:

 private void cbLang_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            ResourceDictionary dict = new ResourceDictionary();

            switch (((sender as ComboBox).SelectedItem as ComboBoxItem).Tag.ToString())
            {
                case "en-US":
                    dict.Source = new Uri("Lang.en-US.xaml", UriKind.Relative);
                    break;
                case "pl-PL":
                    dict.Source = new Uri("Lang.pl-PL.xaml", UriKind.Relative);
                    break;
                default:
                    break;
            }
            this.Resources.MergedDictionaries.Add(dict);
        }
Run Code Online (Sandbox Code Playgroud)

你可以像这样绑定:

 <TextBlock Text="{DynamicResource Username}" VerticalAlignment="Center" />
Run Code Online (Sandbox Code Playgroud)