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文件。

相关问题