windows phone 8 如何保存用户的语言选择并更改整个应用程序的语言?

How to save the language choice of user and change the overall app language in windows phone 8?

我正在尝试本地化我的 windows phone 8.0 应用程序 (SilverLight)。我想根据用户选择更改默认的 Appresources.resx 文件。当用户从设置页面更改语言时,我想将其保存在 IsolatedStorageSettings 中,然后在 InitializeLanguage() 方法中指示保存语言的 Appresources 文件,该方法在我的 app.xaml.cs 构造函数中调用class.

我了解了理论过程,但我无法进一步了解如何处理它。

为了更好地理解我的问题,以下是代码片段。

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;
    }

如果问题太笨拙而无法理解我的目的,我很抱歉,我是这个领域的新手,任何形式的帮助或编辑建议都可以。

用于从 App.xaml.cs class 检索 LocalStorageSettings 的值:

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

App.xaml.cs中,我在方法InitializeLanguage()

的try块下添加了以下代码
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;
}

当用户选择首选语言时,在我的设置页面中:

        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 文件的方式。