Xamrin.Forms 用户界面——控件——Style——设备样式

来源:互联网 发布:淘宝上找货源 编辑:程序博客网 时间:2024/06/07 12:12

设备样式

使用Xamarin.Forms中内置的动态样式

PDF用于离线使用
  • 下载PDF
示例代码:
  • 动态样式
  • 使用样式
相关文章:
  • 文字样式
  • XAML标记扩展
相关API:
  • 资源字典
  • 样式
  • 二传手

让我们知道你对此的感受

最后更新:2016年2月

Xamarin.Forms在Device.Styles类中包含六种动态样式,称为设备样式。

设备样式:

  • BodyStyle
  • CaptionStyle
  • ListItemDetailTextStyle
  • ListItemTextStyle
  • SubtitleStyle
  • TitleStyle

所有六种样式只能应用于Label实例。例如,一个Label显示段落的正文可能会将其Style属性设置为BodyStyle

以下代码示例演示如何在XAML页面中使用设备样式:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.DeviceStylesPage" Title="Device" Icon="xaml.png">    <ContentPage.Resources>        <ResourceDictionary>            <Style x:Key="myBodyStyle" TargetType="Label"              BaseResourceKey="BodyStyle">                <Setter Property="TextColor" Value="Accent" />            </Style>        </ResourceDictionary>    </ContentPage.Resources>    <ContentPage.Content>        <StackLayout Padding="0,20,0,0">            <Label Text="Title style"              Style="{DynamicResource TitleStyle}" />            <Label Text="Subtitle text style"              Style="{DynamicResource SubtitleTextStyle}" />            <Label Text="Body style"              Style="{DynamicResource BodyStyle}" />            <Label Text="Caption style"              Style="{DynamicResource CaptionStyle}" />            <Label Text="List item detail text style"              Style="{DynamicResource ListItemDetailTextStyle}" />            <Label Text="List item text style"              Style="{DynamicResource ListItemTextStyle}" />            <Label Text="No style" />            <Label Text="My body style"              Style="{StaticResource myBodyStyle}" />        </StackLayout>    </ContentPage.Content></ContentPage>

设备样式必须使用DynamicResource标记扩展名。通过更改文本大小的辅助功能设置,可以在iOS中看到样式的动态特性。设备样式的外观在每个平台上是不同的,如下面的屏幕截图所示:

设备的样式也可以通过设置导出BaseResourceKey属性键名称为设备样式。在上面的代码示例中,myBodyStyle继承BodyStyle并设置重音文字颜色。有关动态样式继承的更多信息,请参阅动态样式继承。

以下代码示例演示了C#中的等效页面:

public class DeviceStylesPageCS : ContentPage{    public DeviceStylesPageCS ()    {        var myBodyStyle = new Style (typeof(Label)) {            BaseResourceKey = Device.Styles.BodyStyleKey,            Setters = {                new Setter {                    Property = Label.TextColorProperty,                    Value = Color.Accent                }            }        };        Title = "Device";        Icon = "csharp.png";        Padding = new Thickness (0, 20, 0, 0);        Content = new StackLayout {            Children = {                new Label { Text = "Title style", Style = Device.Styles.TitleStyle },                new Label { Text = "Subtitle style", Style = Device.Styles.SubtitleStyle },                new Label { Text = "Body style", Style = Device.Styles.BodyStyle },                new Label { Text = "Caption style", Style = Device.Styles.CaptionStyle },                new Label { Text = "List item detail text style",                  Style = Device.Styles.ListItemDetailTextStyle },                new Label { Text = "List item text style", Style = Device.Styles.ListItemTextStyle },                new Label { Text = "No style" },                new Label { Text = "My body style", Style = myBodyStyle }            }        };    }}

Style每个Label实例的属性设置为类中的适当属性Devices.Styles

无障碍

设备样式尊重无障碍偏好,从而可访问性偏好在每个平台上改变字体大小会发生变化。因此,为了支持可访问的文本,请确保将设备样式用作应用程序中任何文本样式的基础。

以下屏幕截图演示了每个平台上的设备样式,字体大小最小:

以下屏幕截图演示了每个平台上的设备风格,最大可访问的字体大小:

概要

Xamarin.Forms在类中包含六种动态样式,称为设备样式Devices.Styles。所有六种样式只能应用于Label实例。

阅读全文
0 0
原创粉丝点击