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

隐式样式

使用隐式样式来自定义控件的外观

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

让我们知道你对此的感受

最后更新:2016年2月

隐式样式是同一TargetType的所有控件使用的样式,而不需要每个控件来引用样式。

在XAML中创建一个隐式样式

Style在页面级别声明一个,ResourceDictionary必须在页面中添加一个,然后Style可以包含一个或多个声明ResourceDictionary。通过不指定属性Style使A隐含x:Key。然后,该样式将应用于TargetType与该TargetType值相关的元素,而不是与该值相关的元素。

以下代码示例显示了页面中XAML中声明的隐式样式ResourceDictionary,并应用于页面的Entry实例:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:Styles;assembly=Styles" x:Class="Styles.ImplicitStylesPage" Title="Implicit" Icon="xaml.png">
    <ContentPage.Resources>
        <ResourceDictionary>
            <Style TargetType="Entry">
                <Setter Property="HorizontalOptions" Value="Fill" />
                <Setter Property="VerticalOptions" Value="CenterAndExpand" />
                <Setter Property="BackgroundColor" Value="Yellow" />
                <Setter Property="FontAttributes" Value="Italic" />
                <Setter Property="TextColor" Value="Blue" />
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
        <StackLayout Padding="0,20,0,0">
            <Entry Text="These entries" />
            <Entry Text="are demonstrating" />
            <Entry Text="implicit styles," />
            <Entry Text="and an implicit style override" BackgroundColor="Lime" TextColor="Red" />
            <local:CustomEntry Text="Subclassed Entry is not receiving the style" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

ResourceDictionary定义了一个隐含的多数民众赞成应用于页面的样式Entry实例。的Style用于显示黄色背景上的蓝色文字,同时,也设置其他外观选项。将Style被添加到页面ResourceDictionary而不指定x:Key属性。因此,将它们隐含地Style应用于所有Entry实例,因为它们与正确的TargetType属性匹配Style。但是,这Style不是应用于CustomEntry实例,它是一个子类Entry。这将导致以下屏幕截图中显示的外观:

另外,第四个将隐式样式的属性和属性Entry覆盖到不同的值。BackgroundColorTextColorColor

在控制级别创建隐式样式

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

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:Styles;assembly=Styles" x:Class="Styles.ImplicitStylesPage" Title="Implicit" Icon="xaml.png">
    <ContentPage.Content>
        <StackLayout Padding="0,20,0,0">
            <StackLayout.Resources>
                <ResourceDictionary>
                    <Style TargetType="Entry">
                        <Setter Property="HorizontalOptions" Value="Fill" />
                        ...
                    </Style>
                </ResourceDictionary>
            </StackLayout.Resources>
            <Entry Text="These entries" />
            ...
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

在此示例中,将隐式 Style分配给控件的Resources集合StackLayout。然后可以将隐式样式应用于控件及其子项。

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

在C#中创建隐式样式

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

public class ImplicitStylesPageCS : ContentPage
{
    public ImplicitStylesPageCS ()
    {
        var entryStyle = new Style (typeof(Entry)) {
            Setters = {
                ...
                new Setter { Property = Entry.TextColorProperty, Value = Color.Blue }
            }
        };

        ...
        Resources = new ResourceDictionary ();
        Resources.Add (entryStyle);

        Content = new StackLayout {
            Children = {
                new Entry { Text = "These entries" },
                new Entry { Text = "are demonstrating" },
                new Entry { Text = "implicit styles," },
                new Entry { Text = "and an implicit style override", BackgroundColor = Color.Lime, TextColor = Color.Red },
                new CustomEntry  { Text = "Subclassed Entry is not receiving the style" }
            }
        };
    }
}

构造函数定义应用于页面实例的单个隐式样式Entry。的Style用于显示黄色背景上的蓝色文字,同时,也设置其他外观选项。将Style被添加到页面ResourceDictionary,无需指定key字符串。因此,将它们隐含地Style应用于所有Entry实例,因为它们与正确的TargetType属性匹配Style。但是,这Style不是应用于CustomEntry实例,它是一个子类Entry

概要

一个式是一个真实使用的相同的所有可视元素TargetType,而不需要每个控制以引用的样式。通过不指定属性Style使A 隐含x:Key。相反,x:Key属性将自动变为属性的值TargetType

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值