2013-07-03 59 views
1

好的,所以這個原型的總體目標是在C#中使用WPF創建一個GUI窗口,允許用戶從菜單中更改語言。目前我正嘗試使用App.config文件將窗口的某些組件從英語切換到德語。我知道這不會允許資產使用按鈕進行更改,但這是一個開始。XAML C#本地化問題

我有3個資源文件。一個是默認(英文),另一個是英文,標爲en-US,最後一個是德語,標註爲de-DE。他們都是公共的。

我已經使用了這些兩個視頻作爲引導線:

http://www.youtube.com/watch?v=5MuN6VOw9r4

http://www.youtube.com/watch?v=BK7jp3snwCQ

予設定的標籤的在XAML部分中的內容和兩個按鈕:

<Label Content="{x:Static properties:Resources.Label1}" Height="28" Margin="6,6,79,0" Name="Label1" VerticalAlignment="Top"></Label> 
<Button Content="{x:Static properties:Resources.Button1}" Height="23" HorizontalAlignment="Left" Margin="6,40,0,0" Name="Button1" VerticalAlignment="Top" Width="75"></Button> 
<Button Content="{x:Static properties:Resources.Button2}" Height="23" HorizontalAlignment="Left" Margin="6,69,0,0" Name="Button2" VerticalAlignment="Top" Width="75"></Button> 

這是我在我的App.config文件(de-DE)中應該將編輯的內容更改爲德語:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <appSettings> 
    <add key="Culture" value="de-DE" /> 
    </appSettings> 
</configuration> 

最後,我xaml.cs文件中有以下內容:

using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

using System.Globalization; 
using System.Configuration; 

namespace WPF_Prototype1 
{ 
    /// <summary> 
    /// Interaction logic for Window1.xaml 
    /// </summary> 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      /// This line should allow the program to read the App.config file! 
      Properties.Resources.Culture = new CultureInfo(ConfigurationManager.AppSettings["Culture"]); 
     } 
    } 
} 

爲什麼我的程序仍然沒有改變這些圖標的德國人?我能做些什麼來解決它?

謝謝

+0

如果移動線,以你的App.xaml.cs啓動下,這是否能改變什麼? – user7116

+0

不,我在嘗試使用'CultureInfo'和'「文化」' –

回答

1

我做了第一件事來解決我的問題他們將創建一個新項目。在新項目下,我創建了相同的3個資源文件,並將它們添加到項目屬性中。之後,我創建了相同的App.config文件。

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <appSettings> 
    <add key="Culture" value="de-DE" /> 
    </appSettings> 
</configuration> 

我還留在xaml.cs文件的同一代碼:

Properties.Resources.Culture = new CultureInfo(ConfigurationManager.AppSettings["Culture"]); 

確保所有的名字在我的資源文件進行匹配我在XAML中寫下了下面的代碼後設計窗口:

<Label Content="{x:Static properties:Resources.LabelFirstName}" Height="28" HorizontalAlignment="Left" Margin="33,29,0,0" Name="labelFName" VerticalAlignment="Top" /> 
     <Label Content="{x:Static properties:Resources.LabelLastName}" Height="28" HorizontalAlignment="Left" Margin="34,65,0,0" Name="labelLName" VerticalAlignment="Top" /> 
     <Label Content="{x:Static properties:Resources.LabelAddress}" Height="28" HorizontalAlignment="Left" Margin="35,103,0,0" Name="labelAddress" VerticalAlignment="Top" /> 
     <TextBox Height="23" HorizontalAlignment="Left" Margin="119,26,0,0" Name="textBoxFName" VerticalAlignment="Top" Width="152" /> 
     <TextBox Height="23" HorizontalAlignment="Left" Margin="119,63,0,0" Name="textBoxLName" VerticalAlignment="Top" Width="152" /> 
     <TextBox Height="23" HorizontalAlignment="Left" Margin="119,100,0,0" Name="textBoxAddress" VerticalAlignment="Top" Width="152" /> 
     <Button Content="{x:Static properties:Resources.EnglishButton}" Height="23" HorizontalAlignment="Left" Margin="177,155,0,0" Name="englishButton" VerticalAlignment="Top" Width="94" /> 
     <Button Content="{x:Static properties:Resources.GermanButton}" Height="23" HorizontalAlignment="Left" Margin="177,207,0,0" Name="germanButton" VerticalAlignment="Top" Width="94" /> 

我覺得差異從資源文件是從XAML設計窗口的名稱不同資產的名稱。例如,我的第一個標籤在資源文件中名爲「LabelFirstName」,但在xaml窗口中名爲labelFName。我也沒有拖放任何組件。我確信他們是書面格式。

0

我已經做了在運行時類似的東西,我可以確認作品,變化太大。回顧一下您可能需要獲取INotifyPropertyChanged的代碼。

首先,我對視圖模型的文化也將更改通知,以暴露爲所有文本

public CultureInfo CurrentUICulture 
{ 
    get { return Properties.Resources.Culture; } 
    set 
    { 
    Properties.Resources.Culture = value; 

    OnPropertyChanged("Title"); 
    OnPropertyChanged("Description"); 
    OnPropertyChanged("FarenheitLabel"); 
    OnPropertyChanged("CelsiusLabel"); 
    OnPropertyChanged("Language"); 
    } 
} 

和屬性的屬性(注意評論)

// We could have bound the UI directly to Properties.Resources then we wouldn't be able to 
// NotifyPropertyChanged when the culture changes 
public string Title { get { return Properties.Resources.Title; } } 
public string Description { get { return Properties.Resources.Description; } } 
public string FarenheitLabel { get { return Properties.Resources.FarenheitLabel; } } 
public string CelsiusLabel { get { return Properties.Resources.CelsiusLabel; } } 
public string Language { get { return Properties.Resources.Language; } } 

然後,我把它直接從視圖模型

<Label Grid.Row="5" Grid.ColumnSpan="2" 
      Content="{Binding Language, FallbackValue=Language}"/> 
+0

因此,這些都是在xaml.cs文件的變化,正確的,當一個錯誤?你是否說我必須將文本中的更改編碼到C#文件中?我以前從未使用過INotifyPropertyChanged,是否需要將它包含/導入到文件中? –

+0

我試圖將這些更改添加到xaml.cs文件中,但沒有成功。請記住,我試圖讓語言跨按鈕,標籤,單選按鈕等進行更改。不僅僅是字符串。 –