在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. python:ord()和chr()——字符串和ASCll编码转换

    ord()函数:字符串——>ascll编码 chr()函数:ascll编码——>字符串 #函数 for i in range(ord('d'),ord('f')+1):#拿到d和f对应的a ...

  2. spring data jpa删除的使用方式

    public interface UserRepository extends CrudRepository<User, Long> { Long deleteByLastname(Str ...

  3. Struts2结果页面配置(Result)

    1.全局页面配置 全局结果页面:针对一个包下的所有Action的页面跳转都可生效 <global-results> <result name="xxx">/ ...

  4. #if (DEBUG)

    //DEBUG必须大写,其它是不认的#if (DEBUG)            Console.WriteLine("Debug");#else            Conso ...

  5. ascii编码转utf8编码,适用于python2

    def ascii2utf8(ascii): line = eval(("'" + ascii.strip() + "'")) return line

  6. 【CodeForces - 235C】Cyclical Quest 【后缀自动机】

    题意 给出一个字符串s1和q个询问,每个询问给出一个字符串s2,问这个询问的字符串的所有不同的周期串在s1中出现的次数的和. 分析 对于s1建后缀自动机.对于询问的每个字符串s2,我们按照处理循环串的 ...

  7. java命令查询属性信息

    System.getProperty("user.home")可以查询JAVA系统的user.home属性的值, 除了user.home,还有user.dir, file.sepa ...

  8. RTX二次开发笔记2

    问题一:关于DLL文件的引用 在安装文件夹内 APIObject.dll==>RTXSAPI.dll 服务器API接口 RTXCAPI.DLL ==> 客户端API接口 问题二:RTX二次 ...

  9. Fix: The account is not authorized to log in from this station

    If you have more the one computers running Windows, then its possible to connect them using HomeGrou ...

  10. 商业级别Fortify白盒神器介绍与使用分析

    转自:http://www.freebuf.com/sectool/95683.html 什么是fortify它又能干些什么? 答:fottify全名叫:Fortify SCA ,是HP的产品 ,是一 ...