关于XAML RESX标记扩展的建议 [英] Recommendation on a XAML RESX markup extension

查看:42
本文介绍了关于XAML RESX标记扩展的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多提供XAML标记扩展的RESX标记扩展示例(和Globalizer之类的产品),使您可以动态引用RESX文件中的资源以启用本地化.有没有人看过所有这些以从谷壳中选出小麦?也就是说,有人对XAML的RESX标记扩展有好的建议吗?

There are a number of RESX markup extension samples (and products like Globalizer) that provide XAML markup extensions to allow you to dynamically reference resources in a RESX file to enable localization. Has anybody looked at all these to pick out the wheat from the chaff? That is, anybody have a recommendation on a good RESX markup extension for XAML?

以下是一些可用选项:

  • http://wpflocalizeextension.codeplex.com/
  • http://wpflocalization.codeplex.com/
  • http://www.codeproject.com/Articles/35159/WPF-Localization-Using-RESX-Files
  • http://www.wpftutorial.net/LocalizeMarkupExtension.html
  • http://www.infralution.com/globalizer.html

推荐答案

您不需要任何标记扩展即可完成此操作.您可以使用一个简单的ValueConverter来做到这一点.

You don't need any markup extensions to accomplish this. You can do it with a simple ValueConverter.

  1. 将.resx文件添加到您的项目中(在本示例中,我们将其命名为Resource1.resx,其字符串值为"String1",值为"Hello world!")

  1. Add .resx file to your project (for this example we will call it Resource1.resx, with a string value named "String1" value "Hello world!")

创建一个将字符串转换为本地化字符串的转换器.

Create a converter that will convert a string to a localized string.

namespace WpfApplication1.Converters
{
    public class ResxLocalizationConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string result = Resource1.ResourceManager.GetString(value.ToString());
            if (result == null)
            {
                result = value.ToString();
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

  • 将转换器实例添加到App.xaml

  • Add a converter instance to App.xaml

    <Application x:Class="WpfApplication1.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="Window1.xaml" 
                 xmlns:converters="clr-namespace:WpfApplication1.Converters">
        <Application.Resources>
            <converters:ResxLocalizationConverter x:Key="ResxLocalizationConverter" />
      </Application.Resources>
    </Application>
    

  • 将非本地化的字符串值(或键)放入XAML.

  • Put the non-localized string values (or keys) in your XAML.

    <Window x:Class="WpfApplication1.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window1" Height="300" Width="300">
        <Grid>        
            <TextBlock Text="{Binding Source='String1', Converter={StaticResource ResxLocalizationConverter}}"/>
        </Grid>
    </Window>
    

  • 这将显示"Hello world!"在TextBlock中.

    This will display "Hello world!" in the TextBlock.

    例如,要本地化法语,您只需将Resource1.fr-FR.resx添加到Visual Studio项目中(字符串值名为"String1",值"Salut tout le monde!").这将在法语本地化的PC上显示法语版本,或者您可以使用ResourceManager.GetString(...)重载,以便您明确选择语言.

    To localize for French, for example, you would just need to add Resource1.fr-FR.resx to the Visual Studio project (with a string value named "String1" value "Salut tout le monde!"). This will display the French version on French localized PCs, or you can use the ResourceManager.GetString(...) overload that lets you explicitly select the language.

    这篇关于关于XAML RESX标记扩展的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆