简体   繁体   English

C#Windows Phone 8.1语言选择

[英]C# Windows Phone 8.1 Language selection

I hope this wasn't asked before, I couldn't find an easy solution in MSDN or here. 我希望以前没有问过这个问题,在MSDN或此处找不到简单的解决方案。

The windows phone 8.1 application is deployed in more than one language. Windows Phone 8.1应用程序以多种语言部署。 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. 为此,我在Strings\\en-US\\Ressources.resw使用默认语言(英语),并安装了其中添加了所有其他语言的Multilingual App Toolkit

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. 我创建了一个名为Settings的页面,该页面旨在为用户提供更改语言的可能性。

Now I want to provide the user a ComboBox with all the languages I have translated. 现在,我想为用户提供一个ComboBox其中包含我翻译的所有语言。 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). 默认情况下,所选的ComboBoxItem应显示应用程序的当前语言(而不是系统语言,因为用户可能已经更改了语言)。

Here my solution to the problem, I hope this might be useful to others as well. 在这里,我对问题的解决方案也希望对其他人也有用。

First we create a new struct : 首先,我们创建一个新的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: 然后,在窗体的OnNavigate部分上,添加以下代码:

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. 就这样。

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: 在ComboBox上,将代码更改为:

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 @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: 首先,您必须在XAML框中创建SelectionChanged事件,然后将获得选定的项目,如下所示:

(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). (请注意, oldLang是在更改语言时保存的常量,而changeLang是更改语言的函数)。 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)
    { }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM