如何在 SharpDevelop 4.2 中启动一个国际化的 WPF 项目? [英] How to start an internationalized WPF-Project in SharpDevelop 4.2?

查看:15
本文介绍了如何在 SharpDevelop 4.2 中启动一个国际化的 WPF 项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create a Software, where the User can choose between several Languages.

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

As a start i want to learn how to handle Internationalization, since i have never done that before.

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

As IDE i use SharpDevelop or #develop, however you would spell it. I want to use C# and WPF, since i'm also learning XAML/WPF at the moment.

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

So i create a new WPF-Project in ShardDevelop. On the Main Window i create a ComboBox and a TextBlock.

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

The ComboBox get's two Entries: "German" and "English". The textBlock should show "Hallo Welt!" or "Hello World!", depending on the Language which is selected.

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

Now comes the part where i get stuck. I guess each language get's a separate file in XML/XAML-Style (Makes sense). Where are these files and how are they and their Content loaded so that the Text of the selected Language is loaded?

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

I found several examples but all are something about creating Resource-DLL and using some weird program to decompile them back into a csv-file... i don't get it, isn't there an easier way?

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

I took the next Step. The Text of the TextBlock is now loaded via "{StaticResource Strings.MainForm.hwText}". It looks like this now:

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

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

Also I created one ResourceDictionary for German and one for English which both define the key i used in the TextBlock.

在 Application.Resources 部分中,我默认加载一个 ResourceDictionary.

In the Application.Resources Part i load one of the ResourceDictionary's per default.

现在的问题是:我如何在运行时卸载"这个字典并用另一个替换它?

The Problem now is: How can i "unload" this Dictionary during Runtime and Replace it with the other?

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

Of course i use the SelectionChange-Event of the ComboBox, but what do i do there?

问题解决了!!感谢 kmatyaszek

Problem solved!! Thanks to kmatyaszek

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

Although i changed the Code of the Event-Handler a bit to my needs:

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);
}

推荐答案

如果你创建了两个 ResourceDictionary 文件,你可以通过 DynamicResource 进行绑定.

If you created two ResourceDictionary files you can binding by DynamicResource.

例子:

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

First resource file (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):

Second resource file (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>

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

Set default language in Application resources:

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

假设我们有如下 ComboBox:

Let's say that we have ComboBox like below:

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

代码隐藏的 SelectionChanged:

Code-behind 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" />

这篇关于如何在 SharpDevelop 4.2 中启动一个国际化的 WPF 项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆