2013-05-17 12 views
7

Jak uzyskać dynamicznie zlokalizowany tekst w Windows Phone 8? dowiem się, że jeśli chcę tekst mogę to zrobić przez:Uzyskiwanie dynamicznych lokalizacji ciągów znaków WP8 C#

AppResources.ERR_VERSION_NOT_SUPPORTED 

Ale załóżmy, że mam moje słowa kluczowego z serwera. Wracam tylko ciąg

ERR_VERSION_NOT_SUPPORTED 

Teraz chciałbym uzyskać prawidłowy tekst z AppResources.

Próbowałem następujące:

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

problem jest w tym przykładzie MethodInfo mi jest null ...

ktoś ma jakieś pomysły?

EDIT:

Dziękuję wszystkim za szybkie odpowiedzi. w rzeczywistości Jestem całkiem nowy z C# i zawsze mieszam Properties z powodu składni getters i setters.

mój AppResources wygląda następująco:

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

próbują również uzyskać własność skończyło się dynamicznie nie działa ... i zorientowali się, można korzystać bezpośrednio w ten sposób:

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

Cheers do wszystkich

+0

Czy jesteś pewien, że 'ERR_VERSION_NOT_SUPPORTED' jest metodą? – polkduran

Odpowiedz

15

Możesz uzyskać dostęp do zasobów bez konieczności korzystania z refleksji. Wypróbuj to:

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

tak, dzięki! Właśnie to rozgryzłem :) zobacz moją edycję ... wielkie dzięki! – schurtertom

0

Przede wszystkim AppResources.ERR_VERSION_NOT_SUPPORTED nie jest metodą. Jest to nieruchomość statyczna o pole statyczne. Musisz więc "przeszukać" właściwości statyczne (lub pola). Poniżej przykład właściwości:

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

Dzięki Garath za sugestię! Próbowałem, ale także z tym var prop jest zerowy. Ale w każdym razie znalazłem wreszcie rozwiązanie ... – schurtertom

Powiązane problemy