2013-05-17 23 views
7

Làm cách nào để có được văn bản được bản địa hóa động trong cửa sổ điện thoại 8? tôi tìm ra rằng nếu tôi muốn có một văn bản tôi có thể làm điều này trên:Nhận bản địa hóa chuỗi động WP8 C#

AppResources.ERR_VERSION_NOT_SUPPORTED 

Nhưng chúng ta hãy giả sử tôi có được từ khoá của tôi từ máy chủ. Tôi chỉ lấy lại chuỗi

ERR_VERSION_NOT_SUPPORTED 

Bây giờ tôi muốn nhận được văn bản thích hợp từ AppResources.

Tôi đã thử những điều sau đây:

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

vấn đề là trong ví dụ này MethodInfo mi là null ...

ai có một số ý tưởng?

EDIT:

Cảm ơn tất cả các bạn đã phản ứng nhanh chóng. trong thực tế tôi khá mới với C# và tôi luôn luôn mixup các Properties vì getters và setters cú pháp.

AppResources của tôi trông như thế này:

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

cũng cố gắng để có được tài sản động đã kết thúc không làm việc ... và tôi đã tìm ra tôi có thể trực tiếp sử dụng theo cách này:

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

Cheers cho tất cả

+0

Bạn có chắc chắn rằng 'ERR_VERSION_NOT_SUPPORTED' là một phương pháp không? – polkduran

Trả lời

15

Bạn có thể truy cập tài nguyên mà không phải sử dụng phản chiếu. Hãy thử điều này:

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

có cảm ơn! Tôi chỉ tìm ra ngay bây giờ :) xem chỉnh sửa của tôi ... cảm ơn rất nhiều cho bạn! – schurtertom

0

Trước hết là AppResources.ERR_VERSION_NOT_SUPPORTED không phải là phương pháp. Nó là một thuộc tính tĩnh o trường tĩnh. Vì vậy, bạn cần phải "tìm kiếm" cho các thuộc tính tĩnh (hoặc các trường). Ví dụ bên dưới cho các thuộc tính:

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

Cảm ơn Garath vì đề xuất của bạn! Tôi đã thử nhưng cũng với var prop đó là null. Nhưng Dù sao tôi tìm thấy cuối cùng là giải pháp ... – schurtertom

Các vấn đề liên quan