如何在 WPF/XAML 中更改语言 [英] How to change language in WPF/XAML
问题描述
我必须在 WPF/XAML 中构建一个当前使用 C# 的应用程序.
这段代码我也没有写,所以我无法解释它的一切.
I have to build a application in WPF/XAML that is currently in C#.
I also did not write this code, so I can not explain everything about it.
我需要将应用程序语言更改为用户在主菜单中选择的任何语言的代码.这是在 C# 中工作的代码:
I need the code to change the language of the app to whatever the user chose in the home menu. This is the code that worked in C#:
public static void TranslateForm(string Language, Form f)
{
try
{
string Sprachtext = string.Empty;
clstools tools = new clstools(string.Format(string.Format(clsGlobal.CONNECTION_STRING, clsGlobal.TNSNames,
clsGlobal.DBUser, clsGlobal.DBPassword)), clsGlobal.IDOPERATOR);
//caption text of the form :-)
try
{
if (f.Tag != null)
{
if (tools.IsNumeric(f.Tag.ToString()) == true)
{
Sprachtext = string.Empty;
if (tools.GetLanguageText(Convert.ToInt32(f.Tag.ToString()), Language, ref Sprachtext) == true)
{
f.Text = Sprachtext;
}
}
}
}
catch (Exception)
{
//ignore and proceed
}
foreach (Control c in f.Controls)
{
if (c.Tag != null)
{
if (string.IsNullOrEmpty(c.Tag.ToString()) == false)
{
if (tools.IsNumeric(c.Tag.ToString()) == true)
{
Sprachtext = string.Empty;
if (tools.GetLanguageText(Convert.ToInt32(c.Tag.ToString()), Language, ref Sprachtext) == true)
{
c.Text = Sprachtext;
}
}
}
}
}
}
catch (Exception)
{
//ignore
}
}
如果有问题可以回答这个问题,请告诉我.
If there are questions that answer this please let me know.
另外,如果有什么可以证明这个问题,请告诉我.
Also if there is anything to inprove this question please let me know.
推荐答案
这里是 RessourceDictionnary 使用的一个基本例子,首先是控制器:
Here a basic example of RessourceDictionnary use, first the controller:
public partial class Example : Page
{
public Example() {
InitializeComponent();
// In english
this.Resources.MergedDictionaries.Add("en"));
// In french
//this.Resources.MergedDictionaries.Add("fr"));
}
public ResourceDictionary setLanguageDictionary(string language) {
ResourceDictionary dict = new ResourceDictionary();
switch (language) {
case "en":
dict.Source = new Uri(AppDomain.CurrentDomain.BaseDirectory + "Resources\\StringResources.en-GB.xaml", UriKind.Absolute);
break;
case "fr":
dict.Source = new Uri(AppDomain.CurrentDomain.BaseDirectory + "Resources\\StringResources.fr-FR.xaml",
UriKind.Absolute);
break;
default:
dict.Source = new Uri(AppDomain.CurrentDomain.BaseDirectory + "Resources\\StringResources.en-GB.xaml",
UriKind.Absolute);
break;
}
return dict;
}
}
这里是 xaml ResourceDictionary (StringResources.en-GB.xaml) 之一
Here one of the xaml ResourceDictionary (StringResources.en-GB.xaml)
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SolutionName"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="offline">Offline mode</system:String>
<system:String x:Key="login">Login</system:String>
<system:String x:Key="domain">Domain:</system:String>
<system:String x:Key="username">Username:</system:String>
<system:String x:Key="password">Password:</system:String>
<system:String x:Key="channel">Channel</system:String>
</ResourceDictionary>
最后是视图:
<Page x:Class="SolutionName.Example"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Uuniti.Vue"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Connexion1"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<Grid>
<TextBlock Text="{DynamicResource channel}"/>
</Grid>
</Page>
这篇关于如何在 WPF/XAML 中更改语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!