【问题标题】:How to start an internationalized WPF-Project in SharpDevelop 4.2?如何在 SharpDevelop 4.2 中启动一个国际化的 WPF 项目?
【发布时间】:2012-12-17 12:53:45
【问题描述】:

我想创建一个软件,用户可以在其中选择多种语言。

首先我想学习如何处理国际化,因为我以前从未这样做过。

作为 IDE,我使用 SharpDevelop 或 #develop,但是您可以拼写它。 我想使用 C# 和 WPF,因为我现在也在学习 XAML/WPF。

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

ComboBox 有两个条目:“德语”和“英语”。 文本块应该显示“你好!”或“Hello World!”,具体取决于所选的语言。

现在是我卡住的部分。 我猜每种语言都有一个 XML/XAML 样式的单独文件(有意义)。 这些文件在哪里?如何加载它们及其内容以加载所选语言的文本?

我找到了几个例子,但都是关于创建 Resource-DLL 并使用一些奇怪的程序将它们反编译回 csv 文件的事情......我不明白,没有更简单的方法吗?


我采取了下一步。 TextBlock 的文本现在通过“{StaticResource Strings.MainForm.hwText}”加载。现在看起来像这样:

<TextBlock Text="{StaticResource Strings.MainForm.hwText}" />

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

在 Application.Resources 部分中,我默认加载一个 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);
}

【问题讨论】:

  • 我不知道 SharpDevelop 但在 VS 中你可以很容易地创建 .resx 文件。在属性窗口中,您可以设置本地化。也许 SharpDevelop 也有类似的东西。
  • 谢谢你的提示,我会调查的! :)
  • 如果你找到了告诉我:)

标签: c# wpf xaml internationalization sharpdevelop


【解决方案1】:

如果您创建了两个 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>

第二个资源文件(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>

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

 <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Lang.en-US.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
 </Application.Resources>

假设我们有如下 ComboBox:

<ComboBox Name="cbLang" Margin="2" SelectionChanged="cbLang_SelectionChanged" >
                <ComboBoxItem Content="English" Tag="en-US" />
                <ComboBoxItem Content="Polish" Tag="pl-PL" />
  </ComboBox>

代码隐藏的 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);
        }

你可以这样绑定:

 <TextBlock Text="{DynamicResource Username}" VerticalAlignment="Center" />

【讨论】:

  • 我是不是搞错了什么? (String)Application.Current.FindResource("Username") 始终获取默认值,在&lt;Application.Resources&gt; 中设置(在您的情况下为“用户名”)。有什么建议吗?
  • @Ben 您能否准备示例解决方案并上传到例如飞车?
猜你喜欢
  • 2017-12-12
  • 1970-01-01
  • 2011-04-01
  • 1970-01-01
  • 2021-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多