在AppResources中使用字符串

我正在使用LocalizedResources来获取我的WP 8.1应用程序的本地化。 但是,如果我使用这样的代码:

 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { string xyz = string.Empty; NavigationContext.QueryString.TryGetValue("znam", out xyz); tekstZnamenitosti.Text = AppResources.tekstTrsat; } 

那么我必须有很多if语句来检查每个znamenitost 。 有没有办法使用

 tekstZnamenitosti.Text = AppResources.xyz; 

其中xyz是我之前和之前创建的字符串,是从我导航的页面传递的值。

您可以使用ResourceManager获取AppResources值:

 tekstZnamenitosti.Text = AppResources.ResourceManager.GetString("xyz"); 

只是为了扩展Chris Shao的答案 (+1) – 也许有人觉得它很有用:

为您的资源创建特殊类:

 namespace YourNamespace { public class AppRes { private static ResourceLoader load = new ResourceLoader(); private static string GetProperty([CallerMemberName] string propertyName = null) { return propertyName; } public static string PropertyName { get { return load.GetString(GetProperty()); } } } } 

在App.xaml中添加资源

   // ... rest of the code. 

现在您可以轻松地使用代码中的资源

 string fromResources = AppRes.PropertyName; 

在XAML中:

  

您需要做的一件事是,一旦您将资源添加到Resources.resw ,您必须添加另一行:

 public static string NewPropertyName { get { return load.GetString(GetProperty()); } } 

是的,您可以在App.xaml.cs获得字符串值,例如

  public static string xyz; 

然后,您可以将所需的值保存到您在App.xaml.cs中创建的变量中。

 tekstZnamenitosti.Text = App.xyx; 

您可以在应用程序的任何位置使用此变量。