簡體   English   中英

動態獲取本地化字符串WP8 c#

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM