简体   繁体   中英

Get Localize strings dynamically WP8 c#

How can I get the localized text dynamically in windows phone 8? I find out that if I want a text I can do this over:

AppResources.ERR_VERSION_NOT_SUPPORTED

But let's assume I get my keyword from the server. I only get back the string

ERR_VERSION_NOT_SUPPORTED

Now I would like to get the proper text from AppResources .

I have tried the following:

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);

the problem is in this example the MethodInfo mi is null...

anyone has some ideas?

EDIT:

Thank you all for the fast responses. in Fact I am pretty new with c# and I always mixup the Properties because of the getters and setters syntax.

my AppResources looks like this:

/// <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);
        }
    }
}

also trying to get the property dynamically ended up not working... and I figured out I can directly use this way:

string message = AppResources.ResourceManager.GetString("ERR_VERSION_NOT_SUPPORTED", AppResources.Culture);

Cheers to all

You can access resources without having to use reflection. Try this:

AppResources.ResourceManager.GetString("ERR_VERSION_NOT_SUPPORTED", 
      AppResources.Culture);

First of all AppResources.ERR_VERSION_NOT_SUPPORTED is not a method. It is a static property o static field. So you need to "search" for static properties (or fields). Below example for properties:

string name= "ERR_VERSION_NOT_SUPPORTED";
var prop = typeof(Program).GetProperty(name, BindingFlags.Static);
string message = p.GetValue(null, null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM