天天看点

第十六章:数据绑定(五)字符串格式

字符串格式

上一章中的一些示例程序使用事件处理程序来显示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:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ShowViewValues.ShowViewValuesPage"
             Padding="10, 0">
    <StackLayout>
        <StackLayout VerticalOptions="CenterAndExpand">
            <Label Text="{Binding Source={x:Reference slider},
                                  Path=Value,
                                  StringFormat='The Slider value is {0:F3}'}" />
            <Slider x:Name="slider" />
        </StackLayout>
 
        <StackLayout VerticalOptions="CenterAndExpand">
            <Label Text="{Binding Source={x:Reference entry},
                                  Path=Text,
                                  StringFormat='The Entry text is &#x201C;{0}&#x201D;'}" />
            <Entry x:Name="entry" />
        </StackLayout>
        <StackLayout VerticalOptions="CenterAndExpand">
            <Label Text="{Binding Source={x:Reference stepper},
                                  Path=Value,
                                  StringFormat='The Stepper value is {0}'}" />
            <Stepper x:Name="stepper" />
        </StackLayout>
        <StackLayout VerticalOptions="CenterAndExpand">
            <Label Text="{Binding Source={x:Reference switch},
                                  Path=IsToggled,
                                  StringFormat='The Switch value is {0}'}" />
            <Switch x:Name="switch" />
        </StackLayout>
    </StackLayout>
</ContentPage>           

使用StringFormat时,您需要特别注意逗号,单引号和花括号的位置。

这是结果:

第十六章:数据绑定(五)字符串格式

您可能还记得第5章“处理大小”中的WhatSize程序。该程序在页面上使用SizeChanged事件处理程序以与设备无关的单位显示屏幕的当前宽度和高度。

WhatSizeBindings程序在XAML中完成整个工作。 首先,它将x:Name属性添加到根标记,以便为WhatSizeBindingsPage对象提供页面名称。 三个Label视图在页面中心共享一个水平StackLayout,其中两个绑定到Width和Height属性。 Width和Height属性是get-only,但它们由可绑定属性支持,因此它们在更改时触发PropertyChanged事件:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="WhatSizeBindings.WhatSizeBindingsPage"
             x:Name="page">
    <StackLayout Orientation="Horizontal"
                 Spacing="0"
                 HorizontalOptions="Center"
                 VerticalOptions="Center">
        <StackLayout.Resources>
            <ResourceDictionary>
                <Style TargetType="Label">
                    <Setter Property="FontSize" Value="Large" />
                </Style>
            </ResourceDictionary>
        </StackLayout.Resources>
        <Label Text="{Binding Source={x:Reference page},
                              Path=Width,
                              StringFormat='{0:F0}'}" />
        <!-- Multiplication sign. -->
        <Label Text=" &#x00D7; " />
 
        <Label Text="{Binding Source={x:Reference page},
                               Path=Height,
                               StringFormat='{0:F0}'}" />
    </StackLayout>
</ContentPage>           

以下是本书使用的设备的结果:

第十六章:数据绑定(五)字符串格式

在纵向和横向模式之间转动手机时,显示会发生变化。

或者,可以将StackLayout上的BindingContext设置为引用页面对象的x:Reference标记扩展,并且不需要绑定上的Source设置。

继续阅读