直接在XAML中使用.resw文件中的字符串 [英] Use strings in .resw File directly in XAML

查看:179
本文介绍了直接在XAML中使用.resw文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know the usual way to reference localized strings from a .resw file would be like this:

XAML:

<Button x:Uid="ButtonUid" />

Resources.resw:

Resources.resw:

ButtonUid.Content = "Hello World"

但是也可以这样做吗?

XAML(伪代码):

XAML (pseudo code):

<Button Content = "$buttonLabel" />

Resources.resw:

Resources.resw:

buttonLabel = "Hello World"

我之所以想像第二个例子那样,是因为这是我正在从iOS和Android移植到WP的应用程序.我想将iOS或Android字符串文件转换为.resw语法,但无需遍历每个字符串并添加.Content或.Text或用于其的任何内容.有一个简单的解决方案吗?

The reason why I want to do it like in the second exapmle is because this is an app that I'm porting from iOS and Android to WP. I'd like to convert the iOS or Android strings file to .resw syntax but without going over every single string and adding .Content or .Text or whatever it is used for. Is there an easy solution for that?

推荐答案

您可以使用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();

Then in your App.xaml.cs constructor:
CustomXamlResourceLoader.Current = new XamlResourceLoader();

最后在您的xaml中:
<Button Content = "{CustomResource buttonLabel}" />

And finally in your xaml:
<Button Content = "{CustomResource buttonLabel}" />

这篇关于直接在XAML中使用.resw文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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