Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6533670
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: Android平台

2019-10-28 13:52:08

字符串格式

上一章中的一些示例程序使用事件处理程序来显示Slider和Stepper视图的当前值。如果您尝试从Slider的Value属性定义一个以Label的Text属性为目标的数据绑定,您会发现它有效,但您无法对其进行太多控制。通常,您需要控制数据绑定中所需的任何类型转换或值转换。这将在本章后面讨论。
但是,字符串格式是特殊的。 Binding类具有StringFormat属性,允许您包含整个.NET格式化字符串。几乎总是,这种绑定的目标是Label的Text属性,但绑定源可以是任何类型。
您提供给StringFormat的.NET格式化字符串必须适合调用String.Format静态方法,这意味着它应包含占位符“{0}”,带有或不带有适合源数据类型的格式规范 - 例如“{0:F3}”显示带有三个小数位的double。
在XAML中,这个占位符有点问题,因为花括号可能会被误认为用于分隔标记扩展的花括号。最简单的解决方案是将整个格式字符串放在单引号中。
ShowViewValues程序包含四个显示Slider,Entry,Stepper和Switch当前值的示例。用于显示条目内容的格式字符串中的十六进制代码是“智能引号”的Unicode ID:

点击(此处)折叠或打开

  1. <ContentPage xmlns=""
  2.              xmlns:x=""
  3.              x:Class="ShowViewValues.ShowViewValuesPage"
  4.              Padding="10, 0">
  5.     <StackLayout>
  6.         <StackLayout VerticalOptions="CenterAndExpand">
  7.             <Label Text="{Binding Source={x:Reference slider},
  8.                                   Path=Value,
  9.                                   StringFormat='The Slider value is {0:F3}'}" />
  10.             <Slider x:Name="slider" />
  11.         </StackLayout>
  12.  
  13.         <StackLayout VerticalOptions="CenterAndExpand">
  14.             <Label Text="{Binding Source={x:Reference entry},
  15.                                   Path=Text,
  16.                                   StringFormat='The Entry text is “{0}”'}" />
  17.             <Entry x:Name="entry" />
  18.         </StackLayout>
  19.         <StackLayout VerticalOptions="CenterAndExpand">
  20.             <Label Text="{Binding Source={x:Reference stepper},
  21.                                   Path=Value,
  22.                                   StringFormat='The Stepper value is {0}'}" />
  23.             <Stepper x:Name="stepper" />
  24.         </StackLayout>
  25.         <StackLayout VerticalOptions="CenterAndExpand">
  26.             <Label Text="{Binding Source={x:Reference switch},
  27.                                   Path=IsToggled,
  28.                                   StringFormat='The Switch value is {0}'}" />
  29.             <Switch x:Name="switch" />
  30.         </StackLayout>
  31.     </StackLayout>
  32. </ContentPage>
使用StringFormat时,您需要特别注意逗号,单引号和花括号的位置。
这是结果:
2018_09_25_125653
您可能还记得第5章“处理大小”中的WhatSize程序。该程序在页面上使用SizeChanged事件处理程序以与设备无关的单位显示屏幕的当前宽度和高度。
WhatSizeBindings程序在XAML中完成整个工作。 首先,它将x:Name属性添加到根标记,以便为WhatSizeBindingsPage对象提供页面名称。 三个Label视图在页面中心共享一个水平StackLayout,其中两个绑定到Width和Height属性。 Width和Height属性是get-only,但它们由可绑定属性支持,因此它们在更改时触发PropertyChanged事件:

点击(此处)折叠或打开

  1. <ContentPage xmlns=""
  2.              xmlns:x=""
  3.              x:Class="WhatSizeBindings.WhatSizeBindingsPage"
  4.              x:Name="page">
  5.     <StackLayout Orientation="Horizontal"
  6.                  Spacing="0"
  7.                  HorizontalOptions="Center"
  8.                  VerticalOptions="Center">
  9.         <StackLayout.Resources>
  10.             <ResourceDictionary>
  11.                 <Style TargetType="Label">
  12.                     <Setter Property="FontSize" Value="Large" />
  13.                 </Style>
  14.             </ResourceDictionary>
  15.         </StackLayout.Resources>
  16.         <Label Text="{Binding Source={x:Reference page},
  17.                               Path=Width,
  18.                               StringFormat='{0:F0}'}" />
  19.         <!-- Multiplication sign. -->
  20.         <Label Text=" × " />
  21.  
  22.         <Label Text="{Binding Source={x:Reference page},
  23.                                Path=Height,
  24.                                StringFormat='{0:F0}'}" />
  25.     </StackLayout>
  26. </ContentPage>
以下是本书使用的设备的结果:
2018_09_25_125927
在纵向和横向模式之间转动手机时,显示会发生变化。
或者,可以将StackLayout上的BindingContext设置为引用页面对象的x:Reference标记扩展,并且不需要绑定上的Source设置。


阅读(919) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~