在Xamarin.Forms中,Xaml的模板功能并没有原生WPF丰富,比如Label中虽然有FormattedText可以添加Span来丰富Label的功能,但是下面的Span中的Text并没有绑定属性,无法直接绑定Model的值。

但是FormattedText本身是可以进行绑定的。

那么折中一下,进行数据绑定的时候绑定FormattedText属性,就能临时解决一下问题。

例如有一个Model

 using Xamarin.Forms;

 namespace Baishijiaju.StoreApp.Core.Models
{
public class AvararInfo
{
public string AvararUrl { set; get; } = @"http://192.168.0.228/Common/Images/default-profile-picture.png";
public string Name { set; get; } = "百氏佳居";
public string Company { set; get; } = "哈尔滨市百氏佳居网络科技有限公司";
public FormattedString NameFormattedString
{
get
{
return new FormattedString
{
Spans = {
new Span { Text = "经纪人", ForegroundColor=Color.FromHex("FF6F42"), FontSize= },
new Span { Text = Name,ForegroundColor=Color.FromHex(""), FontSize= } }
};
}
}
}
}

然后我们在进行数据绑定的时候,给Label的FormattedText进行绑定NameFormattedString,Model则正常进行创建就可以了。

         <local:AvatarView
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=1,Constant=-228}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,Property=Y,Factor=0,Constant=12}" >
<local:AvatarView.BindingContext>
<model:AvararInfo />
</local:AvatarView.BindingContext>
</local:AvatarView>

那么我们在Xaml中

 <?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Baishijiaju.StoreApp.Core.Components.AvatarView"
xmlns:effect="clr-namespace:Baishijiaju.StoreApp.Core.Effects" WidthRequest="192" HeightRequest="48">
<ContentView.Content>
<RelativeLayout VerticalOptions="Fill" HorizontalOptions="Fill">
<Label
RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=1,Constant=0}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,Property=Y,Factor=1,Constant=0}"
RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=1,Constant=0}">
<BoxView.Effects>
<effect:BorderEffect BorderColor="White" BorderRadius="48" BorderWidth="1" CoverBackgroundColor="True" />
</BoxView.Effects>
</Label>
<Label Text="{Binding Company}" FontSize="12" TextColor="{StaticResource Gery500}" HorizontalTextAlignment="End"
RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=1,Constant=-48}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,Property=Y,Factor=0,Constant=8}"/>
<Label FormattedText="{Binding NameFormattedString}" HorizontalTextAlignment="End"
RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=1,Constant=-48}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,Property=Y,Factor=0,Constant=26}"/>
<Image Source="{Binding AvararUrl}" WidthRequest="40" HeightRequest="40"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=1,Constant=-44}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,Property=Y,Factor=0,Constant=4}"/>
</RelativeLayout>
</ContentView.Content>
</ContentView>

Label下FormattedText中的Span无法使用Binding的解决方法的更多相关文章

  1. html 中设置span的width完美解决方法

    在默认情况下,由于span是行标签,设置width是无效的.只有改变display的属性,才可以实现设置宽度. 1.初步想法 span{ background-color:#ccc; display: ...

  2. Myeclipse开发环境下文件中出现的提示错误与解决方法:The import javax.servlet cannot be resolved?

    1.开发工具:MyEclipse 2.右击项目  >>  Build Path  >>  Add External Archives (Tomcat  >>  li ...

  3. jquery中checkbox全选失效的解决方法

    这篇文章主要介绍了jquery中checkbox全选失效的解决方法,需要的朋友可以参考下     如果你使用jQuery 1.6 ,代码if ( $(elem).attr(“checked”) ),将 ...

  4. 【配置】检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为【经典】模式)。

      ×   检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为[经典]模式). 我们将ASP.NET程序从IIS6移植到IIS7,可能运行提示以下错误: HTTP 错误 5 ...

  5. Linux中切换用户变成-bash4.1-$的解决方法【转】

    转自 Linux中切换用户变成-bash4.1-$的解决方法 - xia_xia的博客 - 博客频道 - CSDN.NEThttp://blog.csdn.net/xia_xia0919/articl ...

  6. win7下安装Ubuntu后进不去win7的解决方法

    win7下安装Ubuntu后进不去win7的解决方法 刚刚给同学在win7下安装了Ubuntu16.04,结果在安装完后竟然无法在电脑重启后,找到win7的进入选项. 在网上找了找,都不行!就差点重装 ...

  7. Oracle 18c 数据库中scott用户不存在的解决方法

    Oracle 18c 数据库中scott用户不存在的解决方法 注:该文为转载 上面标题可直接跳转 原文地址:http://www.cnblogs.com/zangdalei/p/5482732.htm ...

  8. 记一次SpringBoot 开发中所遇到的坑和解决方法

    记一次SpringBoot 开发中所遇到的坑和解决方法 mybatis返回Integer为0,自动转型出现空指针异常 当我们使用Integer去接受数据库中表的数据,如果返回的数据中为0,那么Inte ...

  9. linux下安装Oracle时交换空间不足的解决方法

    摘:linux下安装Oracle时交换空间不足的解决方法 linux上安装Oracle时交换空间不足的解决办法 增加交换空间有两种方法: 严格的说,在系统安装完后只有一种方法可以增加swap,那就是本 ...

随机推荐

  1. select to object 查询方法

    (1)Select() (2)Where() (3)OrderBy()

  2. [Z]Password-based encryption in Java: salt and key derivation

    http://www.javamex.com/tutorials/cryptography/pbe_salt.shtml 另外,这个网站好像有很多很深入而且很详尽的教程.目测是个类似于官方的或者大牛们 ...

  3. wwwroot文件无读取某个文件夹权限(对IIS_USER增加全部权限)

  4. **深入了解lambda

    之前已经了解过lambda了,但是在学习了闭包之后,我们有必要在探讨一下lambda(匿名函数). 匿名函数本质上就是一个函数,它所抽象出来的东西是一组运算. 它的使用场景就是:你在某处就真的只需要使 ...

  5. MySQL Innodb 神秘消失

    问题描述: 早晨接到 Zabbix 报警,提示 Host: 10.10.1.2, MySQL 主从同步失败. 登录服务器查看具体情况. shell > mysql mysql> show ...

  6. shell编程——流控制case和select

    在shell编程里有时候需要出现交换界面,让使用者来选择要执行的功能,如下面所示,这时候就需要用到case和select进行配合 请选择功能: 1) 退出 2) 系统升级 3) 防火墙配置 4) to ...

  7. visjs使用小记-3.简单网络拓扑图的折叠与展开

    项目需要对节点无限层级查看,大概捣鼓了下,以下demo代码可根据节点的层级顺序,通过节点双击简单实现节点的折叠与展开 <!doctype html> <html> <he ...

  8. 解决linux下root运行Elasticsearch异常

    如果以root身份运行将会出现以下问题 root@yxjay:/opt/elasticsearch-2.3.5/bin# ./elasticsearchException in thread &quo ...

  9. Android自定义view与activity的传值

    昨晚在写团队项目的时候,遇到一个问题,直到今天早上才解决...即在自定义view"转盘"结束转动后获取结果的处理中,我是想吧值传到activity中的一个textview中的,但我 ...

  10. JAVA heap space 内存溢出

    3月28日开始使用JAVA调用SAP RFC后提示内存溢出 尝试增加set java_OPTS.....但仍然提示内存溢出 最终设置方法