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

设备样式

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

PDF用于离线使用
示例代码:
相关文章:
相关API:

让我们知道你对此的感受

最后更新:2016年2月

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

设备样式:

所有六种样式只能应用于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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值