gpt4 book ai didi

c# - 直接在 XAML 中使用 .resw 文件中的字符串

转载 作者:行者123 更新时间:2023-11-30 20:18:40 24 4
gpt4 key购买 nike

我知道从 .resw 文件引用本地化字符串的常用方法是这样的:

XAML:

<Button x:Uid="ButtonUid" />

资源.resw:

ButtonUid.Content = "Hello World"

但是否也可以这样做:

XAML(伪代码):

<Button Content = "$buttonLabel" />

资源.resw:

buttonLabel = "Hello World"

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

最佳答案

您可以使用 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 中:
<Button Content = "{CustomResource buttonLabel}" />

关于c# - 直接在 XAML 中使用 .resw 文件中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40767143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com