在AppResources中使用字符串

4
我正在使用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是我之前创建的一个字符串,其中包含从我导航而来的页面传递的值。

3个回答

6
你可以使用ResourceManager获取AppResources的值:
tekstZnamenitosti.Text = AppResources.ResourceManager.GetString("xyz");

1
不客气。如果对您有帮助,请将其标记为答案。这样更多的人可以获得帮助。谢谢! - Chris Shao
谢谢你提醒我 :) 我尝试了,但它说要等8分钟,结果我就忘了! - RidableCthulu

2

为了扩展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 中添加 Key 到资源中:

<Application.Resources>
    <local:AppRes x:Key="Localized"/>
    // ... rest of the code.

Now you can easily use your Resources from code:

string fromResources = AppRes.PropertyName;

and in XAML:

<TextBlock Text="{Binding PropertyName, Source={StaticResource Localized}}" ...

One thing you have to do is once you add your Resource to Resources.resw, you have to add another line:

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


感谢您的扩展!(+1) - Chris Shao

0

是的,你可以在 App.xaml.cs 中获取字符串值,例如

 public static string xyz;

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

tekstZnamenitosti.Text = App.xyx;

你可以在应用程序的任何地方使用这个变量。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接