C# Windows Phone 8.1 Language selection

。_饼干妹妹 提交于 2019-12-06 14:43:59

You can try something like this

class LanguageCode
{
    string Name { get; set; },
    string CodeName { get; set; }
}

var langs = new List<LanguageCode>();
langs.Add(new LanguageCode() { Name = "English", CodeName = "en-US" });
langs.Add(new LanguageCode() { Name = "Deutsch", CodeName = "de-DE" });
//    ... and so on ...

settings_language_cb.Items.Add(langs);
settings_language_cb.SelectedIndex = 0;

On the ComboBox, change the code to:

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var si = settings_language_cb.SelectedItem as LanguageCode;
    if(si != null) 
        changeLang(si.CodeName);  // changeLang("de-DE");
}

@MrEko

it's easy to get the selected item.

First you have to create a SelectionChanged event in your XAML Combobox and then you will get the selected item as following:

(myXAMLComboBox.SelectedItem as ComboboxItem).Value.ToString();

and here the whole thing in action. (note that oldLang is a constant that I save when I change the language and changeLang is the function that changes the language). And of cause, after changing the language, you have to restart your App, so it takes effect.

private void Page_Settings_LanguageComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  if (oldLang != (PageSettings_Language_cb.SelectedItem as ComboboxItem).Value.ToString())
  {
    try
    {
        changeLang((PageSettings_Language_cb.SelectedItem as ComboboxItem).Value.ToString());
        ShowRestartMessageBox();
    }
    catch (Exception)
    { }
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!