如何解决在运行时绑定Xamarin Forms中标签的FormattedString的跨度
在Xamarin Forms中,我想在collectionView中创建格式化的标签。标签将基于ItemArray中的许多值创建。 如果我在设计中使用xaml创建标签,则效果很好:
<Label VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
<Label.FormattedText>
<FormattedString>
<Span Text="{Binding ItemArray[0]}" TextColor="Blue"/>
<Span Text="{Binding ItemArray[1]}" TextColor="Red"/>
<Span Text="{Binding ItemArray[2]}" TextColor="Blue"/>
</FormattedString>
</Label.FormattedText>
当我尝试在运行时创建标签时,得到的文本是“ {Binding ItemArray [0]}”,而不是实际值
StackLayout ST2 = new StackLayout { Orientation = StackOrientation.Horizontal };
Label LB1 = new Label
{
FormattedText =
new FormattedString
{
Spans =
{
new Span { Text = "{Binding ItemArray[0],Mode=OneWay}",ForegroundColor = Color.Blue },new Span { Text = "{Binding ItemArray[1],ForegroundColor = Color.Red },new Span { Text = "{Binding ItemArray[2],ForegroundColor = Color.Blue }
}
}
};
ST2.Children.Add(LB1);
解决方法
使用SetBinding
var span = new Span() { ForegroundColor = Color.Blue };
span.SetBinding (Span.TextProperty,"ItemArray[0]");
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。