Xamarin.Foms 用户界面——控件——Style——样式继承

来源:互联网 发布:淘宝包含哪些部门 编辑:程序博客网 时间:2024/06/14 23:02

样式继承

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

让我们知道你对此的感受

最后更新:2016年2月

样式可以从其他样式继承,以减少重复和重复使用。

XAML中的风格继承

通过将Style.BasedOn属性设置为现有属性来执行样式继承Style。在XAML中,通过将BasedOn属性设置为StaticResource引用先前创建的标记扩展名来实现Style。在C#中,这是通过将BasedOn属性设置为Style实例来实现的。

从基础样式继承的样式可以包含Setter新属性的实例,或者使用它们来从基本样式重写样式。此外,从基本样式继承的样式必须指向相同的类型,或者是从基本样式定位的类型派生的类型。例如,如果基本样式定位View实例,则基于基本样式的样式可以定位ViewView类派生的实例或类型,例如LabelButton实例。

以下代码演示了XAML页面中的显式样式继承:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.StyleInheritancePage" Title="Inheritance" Icon="xaml.png">    <ContentPage.Resources>        <ResourceDictionary>            <Style x:Key="baseStyle" TargetType="View">                <Setter Property="HorizontalOptions"                        Value="Center" />                <Setter Property="VerticalOptions"                        Value="CenterAndExpand" />            </Style>            <Style x:Key="labelStyle" TargetType="Label"                   BasedOn="{StaticResource baseStyle}">                ...                <Setter Property="TextColor" Value="Teal" />            </Style>            <Style x:Key="buttonStyle" TargetType="Button"                   BasedOn="{StaticResource baseStyle}">                <Setter Property="BorderColor" Value="Lime" />                ...            </Style>        </ResourceDictionary>    </ContentPage.Resources>    <ContentPage.Content>        <StackLayout Padding="0,20,0,0">            <Label Text="These labels"                   Style="{StaticResource labelStyle}" />            ...            <Button Text="So is the button"                    Style="{StaticResource buttonStyle}" />        </StackLayout>    </ContentPage.Content></ContentPage>

baseStyle目标View的情况下,并设置HorizontalOptionsVerticalOptions属性。在baseStyle不直接设置任何控件。相反,labelStylebuttonStyle从中继承,设置其他可绑定属性值。该labelStylebuttonStyle再应用到Label实例,Button例如,通过设置其Style属性。这将导致以下屏幕截图中显示的外观:

可以从显式样式导出隐式样式,但不能从隐式样式派生显式样式。

尊重继承链

样式只能在视图层次结构中从同一级别或更高级别的样式继承。这意味着:

  • 应用程序级资源只能从其他应用程序级资源继承。
  • 页面级资源可以从应用程序级资源和其他页面级资源继承。
  • 控制级资源可以从应用级资源,页级资源和其他控制级资源中继承。

此继承链在以下代码示例中演示:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.StyleInheritancePage" Title="Inheritance" Icon="xaml.png">    <ContentPage.Resources>        <ResourceDictionary>            <Style x:Key="baseStyle" TargetType="View">              ...            </Style>        </ResourceDictionary>    </ContentPage.Resources>    <ContentPage.Content>        <StackLayout Padding="0,20,0,0">            <StackLayout.Resources>                <ResourceDictionary>                    <Style x:Key="labelStyle" TargetType="Label" BasedOn="{StaticResource baseStyle}">                      ...                    </Style>                    <Style x:Key="buttonStyle" TargetType="Button" BasedOn="{StaticResource baseStyle}">                      ...                    </Style>                </ResourceDictionary>            </StackLayout.Resources>            ...        </StackLayout>    </ContentPage.Content></ContentPage>

在这个例子中,labelStyle并且buttonStyle是控制级的资源,同时baseStyle是一个页面级别的资源。但是,虽然labelStylebuttonStyle继承baseStyle,但是不可能baseStyle从视图层次结构中继承labelStyle或者buttonStyle由于它们各自的位置而继承。

C#中的风格继承

下面的代码示例中显示了将Style实例直接分配给Style所需控件的属性的等效C#页面:

public class StyleInheritancePageCS : ContentPage{    public StyleInheritancePageCS ()    {        var baseStyle = new Style (typeof(View)) {            Setters = {                new Setter {                    Property = View.HorizontalOptionsProperty, Value = LayoutOptions.Center },                ...            }        };        var labelStyle = new Style (typeof(Label)) {            BasedOn = baseStyle,            Setters = {                ...                new Setter { Property = Label.TextColorProperty, Value = Color.Teal }            }        };        var buttonStyle = new Style (typeof(Button)) {            BasedOn = baseStyle,            Setters = {                new Setter { Property = Button.BorderColorProperty, Value = Color.Lime },                ...            }        };        ...        Content = new StackLayout {            Children = {                new Label { Text = "These labels", Style = labelStyle },                ...                new Button { Text = "So is the button", Style = buttonStyle }            }        };    }}

baseStyle目标View的情况下,并设置HorizontalOptionsVerticalOptions属性。在baseStyle不直接设置任何控件。相反,labelStylebuttonStyle从中继承,设置其他可绑定属性值。该labelStylebuttonStyle再应用到Label实例,Button例如,通过设置其Style属性。

概要

样式可以从其他样式继承,以减少重复和重复使用。通过将Style.BasedOn属性设置为现有属性来执行样式继承Style

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