2017-01-25 67 views
0

我必須在當前使用C#的WPF/XAML中構建應用程序。
我也沒有寫這段代碼,所以我不能解釋它的一切。如何在WPF/XAML中更改語言

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

如果有問題回答此問題,請告訴我。

此外,如果有什麼可以改善這個問題,請讓我知道。

+0

var culture =「en-GB」; Thread.CurrentThread.CurrentCulture = new CultureInfo(culture); Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture); – jannagy02

回答

0

這裏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> 
+0

感謝您的支持。我會明天嘗試一下,看看它是否會奏效。 – Luca