如何通过字符串代码获取本地化字符串,而不是属性名称?

时间:2012-02-09 04:35:39

标签: windows-phone-7 localization

在WindowsPhone应用程序中,我可以这样使用本地化:

string LocalizedString = LocaleResource.MyPreciousString;

但我可以通过字符串代码而不是属性名称来引用本地化资源吗? 我想做那样的事情:

string LocalizedString = LocaleResource.GetLocalizedStringByCode("MyPreciousString");

1 个答案:

答案 0 :(得分:0)

好的,所以“使用反思,卢克”!这段代码应该有所帮助:

public static string translate(string messageCode)
{
    PropertyInfo info = typeof(Resources.Locale)
        .GetProperty(
            messageCode, 
            BindingFlags.Public | BindingFlags.Static
        )
    ;
    return info != null 
        ? info.GetValue(null, null) as string 
        : messageCode
    ;
}

因此,我们可以在一些Translator类中使用此方法,并通过它代表字符串的代码获取本地化字符串。