7

如何在 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);

问题是在这个例子中MethodInfomi 是空的......

有人有什么想法吗?

编辑:

谢谢大家的快速回复。事实上,我对 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);

为大家喝彩

4

2 回答 2

15

您无需使用反射即可访问资源。尝试这个:

AppResources.ResourceManager.GetString("ERR_VERSION_NOT_SUPPORTED", 
      AppResources.Culture);
于 2013-05-17T09:28:57.900 回答
0

首先AppResources.ERR_VERSION_NOT_SUPPORTED不是方法。它是静态属性或静态字段。因此,您需要“搜索”静态属性(或字段)。以下属性示例:

string name= "ERR_VERSION_NOT_SUPPORTED";
var prop = typeof(Program).GetProperty(name, BindingFlags.Static);
string message = p.GetValue(null, null);
于 2013-05-17T08:57:15.780 回答