【问题标题】:Use strings in .resw File directly in XAML直接在 XAML 中使用 .resw 文件中的字符串
【发布时间】:2016-11-23 14:29:45
【问题描述】:

我知道从 .resw 文件中引用本地化字符串的常用方法如下:

XAML:

<Button x:Uid="ButtonUid" />

Resources.resw:

ButtonUid.Content = "Hello World"

但是是否也可以这样做:

XAML(伪代码):

<Button Content = "$buttonLabel" />

Resources.resw:

buttonLabel = "Hello World"

我想像第二个例子那样做的原因是因为这是我从 iOS 和 Android 移植到 WP 的应用程序。我想将 iOS 或 Android 字符串文件转换为 .resw 语法,但不检查每个字符串并添加 .Content 或 .Text 或其他任何用途。有没有简单的解决方案?

【问题讨论】:

    标签: c# xaml localization uwp-xaml


    【解决方案1】:

    您可以使用CustomXamlResourceLoader

    public class XamlResourceLoader : CustomXamlResourceLoader
    {
        private readonly ResourceLoader _loader;
    
        public XamlResourceLoader()
        {
            _loader = ResourceLoader.GetForViewIndependentUse();
        }
    
        protected override object GetResource(string resourceId, string objectType, string propertyName, string propertyType)
        {
            return _loader.GetString(resourceId) ?? resourceId;
        }
    }
    

    然后在您的 App.xaml.cs 构造函数中:
    CustomXamlResourceLoader.Current = new XamlResourceLoader();

    最后在您的 xaml 中:
    &lt;Button Content = "{CustomResource buttonLabel}" /&gt;

    【讨论】:

    • 我们不能通过这种方法直接使用resx 文件吗?我认为 OP 也这样做了。
    • 是的,我可以 :) 如果您正在阅读本文并有兴趣重新使用您现有的 resx 文件,请查看我的回答:stackoverflow.com/a/35813707/2901207
    【解决方案2】:

    我曾经做过类似的事情,我们将任何新字符串添加到 Android 字符串资源文件中,然后使用自定义构建工具将它们转换为 iOS 和 Windows 格式。

    Android 字符串可能如下所示:

    <string name="Hello">Hello, World!</string>
    

    我们的工具将其转换为 Windows 字符串资源:

    <data name="Hello">
      <value>Hello, World!</value>
    </data>
    

    接下来,添加一个转换器,它对提供的不做任何事情,而是假设它的参数是一个资源ID:

    public sealed class LocalizeConverter : IValueConverter
    {
        private static readonly ResourceLoader Loader = ResourceLoader.GetForViewIndependentUse("/Resources");
    
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            string resourceId = parameter as string;
            return !string.IsNullOrEmpty(resourceId) ? Loader.GetString(resourceId) : DependencyProperty.UnsetValue;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotSupportedException();
        }
    }
    

    现在让该转换器可用于您的 XAML,可能是这样的:

    <Page.Resources>
        <local:LocalizeConverter x:Key="LocalizeConverter" />
    </Page.Resources>
    

    最后,将ButtonContent 属性设置如下:

    <Button
        Content="{x:Bind Converter={StaticResource LocalizeConverter}, ConverterParameter=Hello, Mode=OneTime}"
    />
    

    请注意,我们不向转换器提供任何值。 (在 WPF 中我会创建一个标记扩展。遗憾的是,这个选项在 UWP 中不可用,所以我想出了这个无价值转换器选项作为替代方案。)

    如果你想变得更漂亮,考虑一下:

    <Button
        Content="{x:Bind Language, Converter={StaticResource LocalizeConverter}, ConverterParameter=Hello, Mode=OneWay}"
    />
    

    如果您将资源本地化为其他语言,这可以让您即时更改语言。 (注意Mode=OneWay 而不是Mode=OneTime。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-13
      • 2016-09-12
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      相关资源
      最近更新 更多