标签:c wpf xaml internationalization sharpdevelop
我想创建一个软件,用户可以在几种语言之间进行选择.
作为一个开始,我想学习如何处理国际化,因为我以前从未这样做过.
作为IDE我使用SharpDevelop或#develop,但你会拼写它.
我想使用C#和WPF,因为我现在也在学习XAML / WPF.
所以我在ShardDevelop中创建了一个新的WPF项目.
在主窗口上,我创建了一个ComboBox和一个TextBlock.
ComboBox获得两个条目:“德语”和“英语”.
textBlock应该显示“Hallo Welt!”或“Hello World!”,具体取决于所选的语言.
现在是我陷入困境的部分.
我想每种语言都是XML / XAML-Style(有道理)的单独文件.
这些文件在哪里以及如何加载它们及其内容以便加载所选语言的文本?
我找到了几个例子,但都是关于创建Resource-DLL并使用一些奇怪的程序将它们反编译回csv文件……我不明白,是不是有更简单的方法?
我采取了下一步.
现在通过“{StaticResource Strings.MainForm.hwText}”加载TextBlock的Text.它现在看起来像这样:
<TextBlock Text="{StaticResource Strings.MainForm.hwText}" />
我还为德语创建了一个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);
}
解决方法:
如果您创建了两个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" />
标签:c,wpf,xaml,internationalization,sharpdevelop 来源: https://codeday.me/bug/20190529/1180359.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。