C#Windows Phone 8.1语言选择 [英] C# Windows Phone 8.1 Language selection

查看:109
本文介绍了C#Windows Phone 8.1语言选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望以前没有问过这个问题,在MSDN或此处找不到简单的解决方案.

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

Windows Phone 8.1应用程序以多种语言部署. 为此,我在Strings\en-US\Ressources.resw中使用默认语言(英语)并安装了多语言应用工具包,并在其中添加了所有其他语言.

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

可以被

changeLang("en-US");

此后,我必须重新启动该应用程序(如果不重新启动就无法使其运行).

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

问题是我的实现.我创建了一个名为Settings的页面,我想在其中为用户提供更改语言的可能性.

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

现在,我想为用户提供ComboBox的所有我翻译过的语言.默认情况下,所选的ComboBoxItem应该显示应用程序的当前语言(而不是系统语言,因为用户可能已经更改了语言).

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.

首先,我们创建一个新的struct:

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

然后在表单的OnNavigate部分,我们添加以下代码:

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;

仅此而已.

推荐答案

您可以尝试类似的操作

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

这篇关于C#Windows Phone 8.1语言选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆