Xaml 具有绑定的资源';s转换器
我有几种不同语言的字符串资源。如您所见,它们都以大写字母开头,然后以小写字母开头。那么,有没有任何方法可以在不直接改变资源的情况下将它们全部转换为大写?是否可以在XAML中执行此操作?也许是绑定的转换器的组合Xaml 具有绑定的资源';s转换器,xaml,windows-phone,win-universal-app,uwp,uwp-xaml,Xaml,Windows Phone,Win Universal App,Uwp,Uwp Xaml,我有几种不同语言的字符串资源。如您所见,它们都以大写字母开头,然后以小写字母开头。那么,有没有任何方法可以在不直接改变资源的情况下将它们全部转换为大写?是否可以在XAML中执行此操作?也许是绑定的转换器的组合 答案是你自己说的。使用转换器 Public Namespace Converter Public Class ToUpperValueConverter Implements IValueConverter Public Function Conve
答案是你自己说的。使用转换器
Public Namespace Converter
Public Class ToUpperValueConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object
Dim str = TryCast(value, String)
Return If(String.IsNullOrEmpty(str), String.Empty, str.ToUpper())
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object
Return Nothing
End Function
End Class
编辑
要使用此转换器,您需要对属性使用某种绑定,而不是通常的x:Uid
方式。您不能直接绑定到资源。相反,您需要将资源转换为某种形式的代码隐藏,并通过ViewModel将其绑定。这将引导您完成步骤。但是,您可能需要使用类似的工具来代替PublicResXFileCodeGenerator
恐怕在纯XAML中无法做到这一点。这是同样的事情的C版本:
public class ToUpperConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
string stringValue = value as string;
return string.IsNullOrEmpty(stringValue) ? string.Empty : stringValue.ToUpper();
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotSupportedException();
}
}
要在XAML中引用此内容,请执行以下操作:
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1">
<Page.Resources>
<local:ToUpperConverter x:Key="UpperCaseConverter" />
</Page.Resources>
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="{x:Bind MyString, Converter={StaticResource UpperCaseConverter}}" />
</StackPanel>
</Page>
编辑
在OP的评论中,@RaamakrishnanA询问这将如何与资源协同工作。有点间接是一种方法
在.resw
文件中,为标记
属性提供一个值:
<data name="HelloWorld.Tag">
<value>Hello, world!</value>
</data>
输出:
亲爱的@Raamakrishnan A.,我知道如何编写绑定语句的转换器,但您能提供一些XAML实现吗?例如,我已经知道如何格式化从.resx文件获取的资源?@KonstantinChsherbakov我已经相应地更改了答案。我希望它能帮助你谢谢你的详细回答!事实上,我有点不安,因为它不能通过XAML的预兆,但你给了我一个很好的解决办法)嘿@Konstantin,我只是好奇而已。你能通过Petter的回答访问你的resx/resw文件吗(我猜你的字符串就在这里)?@RaamakrishnanA:这篇文章可能会让你感兴趣:
<data name="HelloWorld.Tag">
<value>Hello, world!</value>
</data>
<TextBlock
x:Name="textBlock"
x:Uid="HelloWorld"
Text="{Binding Tag, ElementName=textBlock, Converter={StaticResource UpperCaseConverter}}"
/>