简体   繁体   English

代码C#中用于Windows Phone 8的多语言应用程序工具包

[英]Multilingual App Toolkit for Windows Phone 8 in Code C#

I want to translate my App with the Multilingual App Toolkit for Windows Phone, but I haven't found anything about how to use the translation in code. 我想使用适用于Windows Phone的Multilingual App Toolkit来翻译我的App,但是我还没有找到有关如何在代码中使用翻译的任何信息。 In XAML it looks like this: 在XAML中,它看起来像这样:

<TextBlock Text="{Binding Path=LocalizedResources.Hallo, Source={StaticResource LocalizedStrings}}" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>

This don't help if I want have a localized string in code. 如果我想要在代码中使用本地化的字符串,这无济于事。

I hope you understand my question, and can help me. 希望您能理解我的问题,并能对我有所帮助。 (I know my English is very bad). (我知道我的英语很不好)。

In App.xaml add key which points to your class LocalizedStrings: 在App.xaml中,添加指向您的类LocalizedStrings的键:

<Application.Resources xmlns:my="clr-namespace:NameSpaceWhereLocalizedStrings">
    <my:LocalizedStrings x:Key="Localized"/>
</Application.Resources>

clr-namespace: - is a namespace where you have your LocalizedStrings (probably your main namespace). clr-namespace:-是一个具有LocalizedStrings的名称空间(可能是您的主要名称空间)。 Then you can use in any other xaml file your LocalizedResources: 然后,您可以在任何其他xaml文件中使用LocalizedResources:

<TextBlock Text="{Binding LocalizedResources.Hallo, Source={StaticResource Localized}}"/>

Hallo - is your variable in AppResources.resx (of course check if it's public). 您好-是您在AppResources.resx中的变量(当然,请检查它是否为公共变量)。 LocalizedResources is a class which you probably have as a default in LocalizedStrings.cs: LocalizedResources是您可能在LocalizedStrings.cs中默认拥有的类:

public class LocalizedStrings
{
  private static AppResources _localizedResources = new AppResources();
  public AppResources LocalizedResources { get { return _localizedResources; } }
}

In code I'm using then: 在代码中,我正在使用:

string myName = AppResources.Hallo;

Note that after adding a variable or changing it you will have to rebuild the project. 请注意,添加或更改变量后,您将必须重新生成项目。

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

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