Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set different localized string in different visual states in WP7 using Blend?

How do I set different localized strings in different visual states in WP7 using Blend without any code behind?

I can set different non-localized strings in different visual states (although it flickers). That works, but how about localized strings?

If I change the string using data binding in Blend, Blend just overrides the data binding in Base state and not the actual state where I'm recording.

EDIT:

This is how I localize my strings:

I have a resources file named AppPresources.resx. Then I would do this in code:

    // setting localized button title
    mainButton.Content = AppResources.MainButtonText;

Then I have a GlobalViewModelLocator from MVVM Light Toolkit with the following Property for Databinding.

    private static AppResources _localizedStrings;
    public AppResources LocalizedStrings
    {
        get
        {
            if (_localizedStrings == null)
            {
                _localizedStrings = new AppResources();
            }
            return _localizedStrings;
        }
    }

And in xaml file:

<Button x:Name="mainButton" Content="{Binding LocalizedStrings.MainButtonText, Mode=OneWay, Source={StaticResource Locator}}" ... />
like image 880
Buju Avatar asked Mar 02 '11 17:03

Buju


1 Answers

What you need to do, is very close to what you're already doing. First, define a class named Resources.cs with following content

public class Resources
{
    private static AppResources resources = new AppResources();

    public AppResources LocalizedStrings
    {
        get
        {
            return resources;
        }
    }
}

This allows us to create a instance of your Resource File in XAML. To do this, open App.xaml and add following

<Application.Resources>
    <local:Resources x:Key="Resources" />
</Application.Resources>

Now when you need to do bindings in your XAML, you do it like this:

<Button Content="{Binding LocalizedStrings.MainButtonText,
                          Source={StaticResource Resources}}" />

What you'll notice is that it doesn't work in Blend, yet. To make it work in Expression Blend, add the following file: DesignTimeResources.xaml in the Properties Folder, and add following content

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:YourNameSpace">
    <local:Resources x:Key="Resources" />
</ResourceDictionary>

Now, you press F6 in Visual Studio to recompile, and voila, your localized strings are available in Expression Blend!

A real-world example from one of my projects:

  • AppResources.cs
  • DesignTimeResources.xaml
  • App.xaml
like image 83
Claus Jørgensen Avatar answered Sep 23 '22 09:09

Claus Jørgensen