[英]Get Localize strings dynamically WP8 c#
如何在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);
为所有人喝彩
您无需使用反射即可访问资源。 尝试这个:
AppResources.ResourceManager.GetString("ERR_VERSION_NOT_SUPPORTED",
AppResources.Culture);
首先, 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.