2

我想本地化我的Windows Phone 8.0應用程序(SilverLight)。我想在用戶選擇時更改默認的Appresources.resx文件。當用戶從設置頁面更改語言時,我想通過IsolatedStorageSettings將其保存,然後在我的構造函數app.xaml.cs類中調用InitializeLanguage()方法中指示保存語言的Appresources文件。如何保存用戶的語言選擇並更改windows phone 8中的整體應用程序語言?

我瞭解了理論過程,但我無法進一步如何處理。

以下是用於更好地理解我的問題的代碼片段。

private void InitializeLanguage() 
     { 
      try 
      { 
       RootFrame.Language = XmlLanguage.GetLanguage(AppResources.ResourceLanguage); 
       FlowDirection flow = (FlowDirection)Enum.Parse(typeof(FlowDirection), AppResources.ResourceFlowDirection); 
       RootFrame.FlowDirection = flow; 
      } 
      catch 
      { 
       if (Debugger.IsAttached) 
       { 
        Debugger.Break(); 
       } 
       throw; 
      } 
     } 

背後,我開始改變了測試目的的文本框,其改變在運行時TextBox語言的語言這樣的設置頁面代碼。

protected override void OnNavigatedTo(NavigationEventArgs e) 
     { 
      base.OnNavigatedTo(e); 

      ChangeLanguageCombo.Items.Add(new LanguageComboBox 
      { 
       Name = "English", 
       Code = "en-US" 
      }); 

      ChangeLanguageCombo.Items.Add(new LanguageComboBox 
      { 
       Name = "Bangla", 
       Code = "bn" 
      }); 
     } 

    public static IsolatedStorageSettings ChangedLanguage = IsolatedStorageSettings.ApplicationSettings; 

    private void ChangeLanguageCombo_OnSelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     var languageComboBox = ChangeLanguageCombo.SelectedItem as LanguageComboBox; 
     ApplyChange(new CultureInfo(languageComboBox.Code.ToString())); 
     //now I want to save the user choice to the `IsolatedStorageSettings ChangedLanguage` and restart the app to take place the changes. 
     MessageBox.Show("Restart"); 
     //after restart I want to indicate the Appresources file to the new selected one,(in InitializeLang() method) RootFrame.Language = XmlLanguage.GetLanguage(AppResources.ResourceLanguage); in this line 
     } 

    } 

    private void ApplyChange(CultureInfo culInfo) 
    { 
     Thread.CurrentThread.CurrentCulture = culInfo; 
     Thread.CurrentThread.CurrentUICulture = culInfo; 
     textBlockHello.Text = AppResources.Salutation; 
    } 

我很抱歉,如果這個問題是太笨拙明白我的目的,我在這個領域,任何形式的幫助或編輯的建議將做新的。

回答

2

對於從App.xaml.cs類檢索LocalStorageSettings的價值:

string value= IsolatedStorageSettings.ApplicationSettings["userData"] as string; 

App.xaml.cs我添加以下代碼下的方法InitializeLanguage()

private void InitializeLanguage() 
{ 
    try 
    { 
     if (IsolatedStorageSettings.ApplicationSettings.Contains("selectedLang")) 
     { 
      var changedLang = IsolatedStorageSettings.ApplicationSettings["selectedLang"] as string; 
      if (changedLang != null) ApplyChange(new CultureInfo(changedLang)); 
     }    
    } 
    //rest of the part in this method remained same 
} 
private void ApplyChange(CultureInfo culInfo) 
{ 
    Thread.CurrentThread.CurrentCulture = culInfo; 
    Thread.CurrentThread.CurrentUICulture = culInfo; 
} 

的try塊在我的設置頁面當用戶選擇首選語言時:

 public static IsolatedStorageSettings ChangedLanguage = IsolatedStorageSettings.ApplicationSettings; 
     private void ChangeLanguageCombo_OnSelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      var languageComboBox = ChangeLanguageCombo.SelectedItem as LanguageComboBox; 

      if (languageComboBox != null) 
      { 
       if (!ChangedLanguage.Contains("selectedLang")) 
       { 
        ChangedLanguage.Add("selectedLang", languageComboBox.Code.ToString()); 
       } 
       else 
       { 
        ChangedLanguage["selectedLang"] = languageComboBox.Code.ToString(); 
       } 
       ChangedLanguage.Save(); 
       MessageBox.Show("Restart"); 
      } 

     } 

重新啓動應用後,默認Appresources文件將成爲新語言的Appresources文件,因爲它保存在IsolatedStorageSettings和應用啓動App.xaml.cs頁面調用InitializeLanguage()方法。
因此,當用戶從設置頁面更改我的應用程序的語言時,我可以如何更改默認的Appresources文件。

相關問題