开发者

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

开发者 https://www.devze.com 2023-02-14 14:59 出处:网络
How do I set different localized strings in different visual states in WP7 using Blend without any code behind?

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 name开发者_C百科d 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}}" ... />


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
0

精彩评论

暂无评论...
验证码 换一张
取 消