簡體   English   中英

如何在WPF / XAML中更改語言

[英]How to change language in WPF/XAML

我必須在WPF / XAML中構建當前在C#中的應用程序。
我也沒有編寫此代碼,因此無法解釋所有內容。

我需要代碼將應用程序的語言更改為用戶在主菜單中選擇的任何語言。 這是在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
    }
}

如果有任何問題可以回答,請告訴我。

另外,如果有什么可以證明這個問題的信息,請告訴我。

這里是RessourceDictionnary使用的基本示例,首先是控制器:

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)之一

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM