C# Windows Phone 8.1 Language selection

自闭症网瘾萝莉.ら 提交于 2020-01-03 01:20:13

问题


I hope this wasn't asked before, I couldn't find an easy solution in MSDN or here.

The windows phone 8.1 application is deployed in more than one language. To do so I use the default language (english) in Strings\en-US\Ressources.resw and installed the Multilingual App Toolkit with all further languages added there.

To change the language, I have the following code:

private void changeLang(string cul)
{
    Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = cul;

    Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
    Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();

    if (Frame != null)
        Frame.Navigate(typeof(MainPage));
}

which can be called by

changeLang("en-US");

After that I have to restart the application (couldn't make it work without restart yet).

The problem is my implementation. I created a page called Settings where I want to provide the user the possibility to change the language.

Now I want to provide the user a ComboBox with all the languages I have translated. By default the selected ComboBoxItem should show the current language of the application (not the Systems language, as the user might already have had changed the language).

Here my solution to the problem, I hope this might be useful to others as well.

First we create a new struct:

public class ComboboxItem
{
    public string Text { get; set; }
    public object Value { get; set; }

    public override string ToString()
    {
        return Text;
    }
}

Then on the OnNavigate part on the Form we add the following code:

settings_language_cb.Items.Add(new ComboboxItem { Text = "Deutsch", Value = "de-DE" });
settings_language_cb.Items.Add(new ComboboxItem { Text = "English", Value = "en-US" });

var curLangItem = settings_language_cb.Items.SingleOrDefault(x => (x as ComboboxItem).Value.ToString() == CultureInfo.CurrentCulture.Name);

settings_language_cb.SelectedItem = curLangItem;
settings_language_cb.PlaceholderText = (curLangItem as ComboboxItem).Text;

And that's all.


回答1:


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



回答2:


@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)
    { }
  }
}


来源:https://stackoverflow.com/questions/26074531/c-sharp-windows-phone-8-1-language-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!