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
实例,则基于基本样式的样式可以定位View
从View
类派生的实例或类型,例如Label
和Button
实例。
以下代码演示了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
的情况下,并设置HorizontalOptions
和VerticalOptions
属性。在baseStyle
不直接设置任何控件。相反,labelStyle
并buttonStyle
从中继承,设置其他可绑定属性值。该labelStyle
和buttonStyle
再应用到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
是一个页面级别的资源。但是,虽然labelStyle
并buttonStyle
继承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
的情况下,并设置HorizontalOptions
和VerticalOptions
属性。在baseStyle
不直接设置任何控件。相反,labelStyle
并buttonStyle
从中继承,设置其他可绑定属性值。该labelStyle
和buttonStyle
再应用到Label
实例,Button
例如,通过设置其Style
属性。
概要
样式可以从其他样式继承,以减少重复和重复使用。通过将Style.BasedOn
属性设置为现有属性来执行样式继承Style
。
- Xamarin.Foms 用户界面——控件——Style——样式继承
- Xamarin.Forms 用户界面——控件——Style——显示样式
- Xamarin.Forms 用户界面——控件——Style——全局样式
- Xamarin.Forms 用户界面——控件——Style——动态样式
- Xamarin.Forms 用户界面——控件
- Xamarin.Forms 用户界面——控件——Style——介绍
- Xamarin.Forms 用户界面——控件——Style——隐式样式
- Xamrin.Forms 用户界面——控件——Style——设备样式
- Xamarin.Forms 用户界面——控件——页面
- Xamarin.Forms 用户界面——控件——布局
- Xamarin.Forms 用户界面——控件——View
- Xamarin.Forms 用户界面——控件——Cells
- Xamarin.Forms 用户界面——控件——Images
- Xamarin.Forms 用户界面——控件——Layouts
- Xamarin.Forms 用户界面——控件——ListView
- Xamarin.Forms 用户界面——控件——Picker
- Xamarin.Forms 用户界面——控件——Styles
- Xamarin.Forms 用户界面——控件——TableView
- jdbcTemplate 批量操作
- python中文件读取
- 【BLE4.0】CC254x-修改广播时间
- 微信小程序开发
- F
- Xamarin.Foms 用户界面——控件——Style——样式继承
- javascript深入理解js闭包
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 2017年8月9日训练日记
- 刷题——6092 Rikka with Subset
- 5-4 畅通工程之最低成本建设问题 (30分)
- JavaScript作用域(一)
- Vim操作拾遗
- Mysql 开启视图权限与创建视图教程