如何在 Windows Phone 8 中动态获取本地化文本?我发现如果我想要一个文本,我可以这样做:
AppResources.ERR_VERSION_NOT_SUPPORTED
但是让我们假设我从服务器获取我的关键字。我只取回字符串
ERR_VERSION_NOT_SUPPORTED
现在我想从AppResources
.
我尝试了以下方法:
string methodName = "ERR_VERSION_NOT_SUPPORTED";
AppResources res = new AppResources();
//Get the method information using the method info class
MethodInfo mi = res.GetType().GetMethod(methodName);
//Invoke the method
// (null- no parameter for the method call
// or you can pass the array of parameters...)
string message = (string)mi.Invoke(res, null);
问题是在这个例子中MethodInfo
mi 是空的......
有人有什么想法吗?
编辑:
谢谢大家的快速回复。事实上,我对 c# 很陌生,而且Properties
由于 getter 和 setter 语法,我总是混淆。
我的AppResources
样子是这样的:
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class AppResources
{
...
/// <summary>
/// Looks up a localized string similar to This version is not supported anymore. Please update to the new version..
/// </summary>
public static string ERR_VERSION_NOT_SUPPORTED
{
get
{
return ResourceManager.GetString("ERR_VERSION_NOT_SUPPORTED", resourceCulture);
}
}
}
还试图动态获取属性最终无法正常工作......我发现我可以直接使用这种方式:
string message = AppResources.ResourceManager.GetString("ERR_VERSION_NOT_SUPPORTED", AppResources.Culture);
为大家喝彩