如何在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为null ...
任何人都有一些想法?
修改
谢谢大家的快速回复。
事实上,我对c#很新,因为getter和setter语法,我总是混淆Properties
。
我的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);
为所有人欢呼
答案 0 :(得分:15)
您无需使用反射即可访问资源。试试这个:
AppResources.ResourceManager.GetString("ERR_VERSION_NOT_SUPPORTED",
AppResources.Culture);
答案 1 :(得分:0)
首先,AppResources.ERR_VERSION_NOT_SUPPORTED
不是一种方法。它是一个静态属性o static field。因此,您需要“搜索”静态属性(或字段)。下面是属性示例:
string name= "ERR_VERSION_NOT_SUPPORTED";
var prop = typeof(Program).GetProperty(name, BindingFlags.Static);
string message = p.GetValue(null, null);