Xamarin.Forms 用户界面——控件——Style——显示样式

显式样式

使用显式样式来定制特定控件的外观

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

让我们知道你对此的感受

最后更新:2016年2月

显式样式是通过设置其Style属性来选择性地应用于控件的。

在XAML中创建显式样式

Style在页面级别声明一个,ResourceDictionary必须在页面中添加一个,然后Style可以包含一个或多个声明ResourceDictionary。一个Style是由明确给予其声明的x:Key属性,这使得它在描述性的密钥ResourceDictionary。然后必须通过设置其属性将显式样式应用于特定的视觉元素Style

以下代码示例显示了页面中XAML中声明的应用于页面实例的显式样式:ResourceDictionaryLabel

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.ExplicitStylesPage" Title="Explicit" Icon="xaml.png">
    <ContentPage.Resources>
        <ResourceDictionary>
            <Style x:Key="labelRedStyle" TargetType="Label">
                <Setter Property="HorizontalOptions"
                        Value="Center" />
                <Setter Property="VerticalOptions"
                        Value="CenterAndExpand" />
                <Setter Property="FontSize" Value="Large" />
                <Setter Property="TextColor" Value="Red" />
            </Style>
            <Style x:Key="labelGreenStyle" TargetType="Label">
                ...
                <Setter Property="TextColor" Value="Green" />
            </Style>
            <Style x:Key="labelBlueStyle" TargetType="Label">
                ...
                <Setter Property="TextColor" Value="Blue" />
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
        <StackLayout Padding="0,20,0,0">
            <Label Text="These labels"
                   Style="{StaticResource labelRedStyle}" />
            <Label Text="are demonstrating"
                   Style="{StaticResource labelGreenStyle}" />
            <Label Text="explicit styles,"
                   Style="{StaticResource labelBlueStyle}" />
            <Label Text="and an explicit style override"
                   Style="{StaticResource labelBlueStyle}"
                   TextColor="Teal" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

ResourceDictionary定义了三个明确的被应用到页面的样式Label实例。每个Style用于以不同的颜色显示文本,同时还设置字体大小和水平和垂直布局选项。每个StyleLabel通过Style使用StaticResource标记扩展名设置其属性来应用于其他。这将导致以下屏幕截图中显示的外观:

另外,最后Label一个Style应用到它,但也覆盖TextColor属性到一个不同的Color值。

在控制级别创建显式样式

除了在页面级别创建显式样式之外,还可以在控件级别创建它们,如以下代码示例所示:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.ExplicitStylesPage" Title="Explicit" Icon="xaml.png">
    <ContentPage.Content>
        <StackLayout Padding="0,20,0,0">
            <StackLayout.Resources>
                <ResourceDictionary>
                    <Style x:Key="labelRedStyle" TargetType="Label">
                      ...
                    </Style>
                    ...
                </ResourceDictionary>
            </StackLayout.Resources>
            <Label Text="These labels" Style="{StaticResource labelRedStyle}" />
            ...
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

在这个例子中,显式 Style实例被分配给控件的Resources集合StackLayout。然后可以将样式应用于控件及其子项。

有关在应用程序中创建样式的信息ResourceDictionary,请参阅全局样式

在C#中创建一个显式样式

Style实例可以Resources通过创建新的C#添加到页面的集合中ResourceDictionary,然后通过将Style实例添加到ResourceDictionary,如下面的代码示例所示:

public class ExplicitStylesPageCS : ContentPage
{
    public ExplicitStylesPageCS ()
    {
        var labelRedStyle = new Style (typeof(Label)) {
            Setters = {
                ...
                new Setter { Property = Label.TextColorProperty, Value = Color.Red  }
            }
        };
        var labelGreenStyle = new Style (typeof(Label)) {
            Setters = {
                ...
                new Setter { Property = Label.TextColorProperty, Value = Color.Green }
            }
        };
        var labelBlueStyle = new Style (typeof(Label)) {
            Setters = {
                ...
                new Setter { Property = Label.TextColorProperty, Value = Color.Blue }
            }
        };

        Resources = new ResourceDictionary ();
        Resources.Add ("labelRedStyle", labelRedStyle);
        Resources.Add ("labelGreenStyle", labelGreenStyle);
        Resources.Add ("labelBlueStyle", labelBlueStyle);
        ...

        Content = new StackLayout {
            Children = {
                new Label { Text = "These labels",
                            Style = (Style)Resources ["labelRedStyle"] },
                new Label { Text = "are demonstrating",
                            Style = (Style)Resources ["labelGreenStyle"] },
                new Label { Text = "explicit styles,",
                            Style = (Style)Resources ["labelBlueStyle"] },
                new Label { Text = "and an explicit style override",
                            Style = (Style)Resources ["labelBlueStyle"], TextColor = Color.Teal }
            }
        };
    }
}

构造函数定义了应用于页面实例的三种显式样式Label。每个显式 Style添加到ResourceDictionary使用该Add方法,指定一个key字符串来引用该Style实例。每个StyleLabel通过设置其Style属性应用于不同的。

但是,在ResourceDictionary这里使用没有任何优势。相反,Style实例可以直接分配给Style所需可视元素的属性,并且ResourceDictionary可以将其删除,如以下代码示例所示:

public class ExplicitStylesPageCS : ContentPage
{
    public ExplicitStylesPageCS ()
    {
        var labelRedStyle = new Style (typeof(Label)) {
            ...
        };
        var labelGreenStyle = new Style (typeof(Label)) {
            ...
        };
        var labelBlueStyle = new Style (typeof(Label)) {
            ...
        };
        ...
        Content = new StackLayout {
            Children = {
                new Label { Text = "These labels", Style = labelRedStyle },
                new Label { Text = "are demonstrating", Style = labelGreenStyle },
                new Label { Text = "explicit styles,", Style = labelBlueStyle },
                new Label { Text = "and an explicit style override", Style = labelBlueStyle,
                            TextColor = Color.Teal }
            }
        };
    }
}

构造函数定义了应用于页面实例的三种显式样式Label。每个Style用于以不同的颜色显示文本,同时还设置字体大小和水平和垂直布局选项。每个StyleLabel通过设置其Style属性应用于不同的。另外,最后Label一个Style应用到它,但也覆盖TextColor属性到一个不同的Color值。

概要

Style是由明确的通过给予其声明的x:Key属性,然后选择性地通过设置其它应用到控制Style性能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值