如何使Label显示带有不同颜色字母的FormattedString? [英] How to make a Label display a FormattedString with letters of different color?
问题描述
我正在使用FormattedString在Xamarin.Forms的Label上显示自定义文本.我试图实现的是更改一个或多个元素的颜色,例如: $$ $$.但是,即使我更改颜色,标签也只会显示所有具有相同颜色的美元符号:$$$$
I am using a FormattedString to Display a customized text on a Label on Xamarin.Forms. What I am trying to achieve is to change the color of one or more of the elements, for example: $$$$. But even though I am changing the color the Label just displays all the dollar symbols with the same color: $$$$
这是视图上的标签:
<Label Text="{Binding AveragePrice, StringFormat='{0}'}" HorizontalTextAlignment="Center" />
这是绑定到ViewModel上标签文本的属性的代码
And this is the code of the property bound to the label text on the ViewModel
public FormattedString AveragePrice
{
get
{
return new FormattedString
{
Spans =
{
new Span { Text = "$", ForegroundColor=Color.Black },
new Span { Text = "$", ForegroundColor=Color.Black },
new Span { Text = "$", ForegroundColor=Color.Gray },
new Span { Text = "$", ForegroundColor=Color.Gray }
}
};
}
}
为什么此代码不会更改美元符号的颜色?我该如何实现?
Why this code doesn't change the color of the dollar symbols? How can I make it happen?
推荐答案
将AveragePrice绑定到FormattedText属性,然后删除StringFormat.
Bind AveragePrice to the FormattedText property, and remove the StringFormat.
<Label FormattedText="{Binding AveragePrice}" HorizontalTextAlignment="Center" />
这篇关于如何使Label显示带有不同颜色字母的FormattedString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!