RidableCthulu
RidableCthulu

Reputation: 471

Using a string in AppResources

I'm using LocalizedResources to get localization for my WP 8.1 app. But if I used a code like this:

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

then I would have to have many if statements to check for each znamenitost. Is there a way to use

tekstZnamenitosti.Text = AppResources.xyz;

where xyz is a string that I made before and in it is the value passed from a page which I navigated from.

Upvotes: 4

Views: 1270

Answers (3)

Kulasangar
Kulasangar

Reputation: 9434

Yeah you can have the string value in the App.xaml.cs such as

 public static string xyz;

Then you can save the value you wanted to the variable you created in the App.xaml.cs.

tekstZnamenitosti.Text = App.xyx;

You could use this variable anywhere in the app.

Upvotes: 0

Romasz
Romasz

Reputation: 29792

Just to extend Chris Shao's answer(+1) a little - maybe somebody find it usefull:

Create special class for your Resources:

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

In App.xaml add Key to resources:

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

Upvotes: 2

Chris Shao
Chris Shao

Reputation: 8231

You can get the AppResources value using ResourceManager:

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

Upvotes: 6

Related Questions