2010-03-30 97 views
0

我正在嘗試在Silverlight 4.0中編寫多語言應用程序,並且我可以從SampleData xaml文件中的動態文本開始替換靜態文本。以下是我有:動態設置TextBlock的文本綁定

我的數據庫

<SampleData:something xmlns:SampleData="clr-namespace:Expression.Blend.SampleData.MyDatabase" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> 
    <SampleData:something.mysystemCollection> 
    <SampleData:mysystem ID="1" English="Menu" German="Menü" French="Menu" Spanish="Menú" Swedish="Meny" Italian="Menu" Dutch="Menu" /> 
    </SampleData:something.mysystemCollection> 
</SampleData:something> 

我的用戶

<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" 
    x:Class="Something.MyUC" d:DesignWidth="1000" d:DesignHeight="600"> 
    <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource MyDatabase}}"> 
     <Grid Height="50" Margin="8,20,8,0" VerticalAlignment="Top" d:DataContext="{Binding mysystemCollection[1]}" x:Name="gTitle"> 
      <TextBlock x:Name="Title" Text="{Binding English}" TextWrapping="Wrap" Foreground="#FF00A33D" TextAlignment="Center" FontSize="22"/> 
     </Grid> 
    </Grid> 
</UserControl> 

正如你所看到的,我有7種語言,我想處理。現在,這加載我的文字的英文版本就好了。今天我花了很多時間試圖弄清楚如何改變我的代碼中的綁定,以便在需要時將其交換出來(可以說當我通過下拉菜單更改語言時)。

任何幫助將是偉大的!

回答

1

你正在走這個錯誤的路。在Silverlight中進行本地化的最佳做法是使用包含已翻譯關鍵字的資源文件。下面是一些關於這個更多的信息:

http://msdn.microsoft.com/en-us/library/cc838238%28VS.95%29.aspx

編輯:

這裏是我使用一個輔助類來保存翻譯的字符串的例子。這些翻譯可以從幾乎任何地方加載。靜態資源文件,XML,數據庫或其他。我做得很匆忙,所以它不是很穩定。它只能在英語和瑞典語之間切換。

XAML:

<UserControl x:Class="SilverlightApplication13.MainPage" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:local="clr-namespace:SilverlightApplication13" 
      mc:Ignorable="d" 
      d:DesignWidth="640" 
      d:DesignHeight="480"> 

    <UserControl.Resources> 
     <local:TranslationHelper x:Key="TranslationHelper"></local:TranslationHelper> 
    </UserControl.Resources> 

    <Grid x:Name="LayoutRoot"> 
     <StackPanel> 

      <TextBlock Margin="10" 
         Text="{Binding Home, Source={StaticResource TranslationHelper}}"></TextBlock> 

      <TextBlock Margin="10" 
         Text="{Binding Contact, Source={StaticResource TranslationHelper}}"></TextBlock> 

      <TextBlock Margin="10" 
         Text="{Binding Links, Source={StaticResource TranslationHelper}}"></TextBlock> 

      <Button Content="English" 
        HorizontalAlignment="Left" 
        Click="BtnEnglish_Click" 
        Margin="10"></Button> 

      <Button Content="Swedish" 
        HorizontalAlignment="Left" 
        Click="BtnSwedish_Click" 
        Margin="10"></Button> 
     </StackPanel> 
    </Grid> 
</UserControl> 

代碼隱藏+ TranslationHelper類:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Windows.Threading; 
using System.ComponentModel; 

namespace SilverlightApplication13 
{ 
    public partial class MainPage : UserControl 
    { 
     public MainPage() 
     { 
      InitializeComponent(); 

      //Default 
      (this.Resources["TranslationHelper"] as TranslationHelper).SetLanguage("en-US"); 
     } 

     private void BtnEnglish_Click(object sender, RoutedEventArgs e) 
     { 
      (this.Resources["TranslationHelper"] as TranslationHelper).SetLanguage("en-US"); 
     } 

     private void BtnSwedish_Click(object sender, RoutedEventArgs e) 
     { 
      (this.Resources["TranslationHelper"] as TranslationHelper).SetLanguage("sv-SE"); 
     } 
    } 

    public class TranslationHelper : INotifyPropertyChanged 
    { 
     private string _Contact; 

     /// <summary> 
     /// Contact Property 
     /// </summary> 
     public string Contact 
     { 
      get { return _Contact; } 
      set 
      { 
       _Contact = value; 
       OnPropertyChanged("Contact"); 
      } 
     } 

     private string _Links; 

     /// <summary> 
     /// Links Property 
     /// </summary> 
     public string Links 
     { 
      get { return _Links; } 
      set 
      { 
       _Links = value; 
       OnPropertyChanged("Links"); 
      } 
     } 

     private string _Home; 

     /// <summary> 
     /// Home Property 
     /// </summary> 
     public string Home 
     { 
      get { return _Home; } 
      set 
      { 
       _Home = value; 
       OnPropertyChanged("Home"); 
      } 
     } 



     public TranslationHelper() 
     { 
      //Default 
      SetLanguage("en-US"); 
     } 

     public void SetLanguage(string cultureName) 
     { 
      //Hard coded values, need to be loaded from db or elsewhere 

      switch (cultureName) 
      { 
       case "sv-SE": 
        Contact = "Kontakt"; 
        Links = "Länkar"; 
        Home = "Hem"; 
        break; 

       case "en-US": 
        Contact = "Contact"; 
        Links = "Links"; 
        Home = "Home"; 
        break; 

       default: 
        break; 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     protected virtual void OnPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 
} 
+0

這是否允許我動態設置語言,而無需重新加載應用程序?在MSDN上的示例中,它將用戶重定向到新頁面 – 2010-03-30 13:33:46

+1

Aha。現在我明白你的所作所爲。不,使用靜態資源時,用戶將無法實時切換語言。如果這是你需要的,你應該使用數據綁定。但是你的代碼示例對我來說仍然看起來過於複雜。我正在準備自己的代碼示例。我會很快在這裏發佈。 – 2010-03-30 14:05:03

+0

當你加載一個可以共享資源的孩子時,有沒有什麼辦法讓這個工作起作用?我試圖把它放在一個類文件中,但是當我的UC在一個子文件夾中時它不想加載它。 – 2010-03-30 15:23:37

2

這聽起來像你正在尋找這樣的代碼:

Title.SetBinding(TextProperty, new Binding { Path = new PropertyPath(language) }); 

它所做的就是爲您創建請求的語言和新的綁定,並用它來代替舊的綁定爲標題的Text屬性。

+0

最後我用這個問題,以及一些零部件。太糟糕了,我無法將兩個答案都標記爲已選中。 – 2010-03-30 18:57:40