样式继承
样式可以从其他样式继承,以减少重复和重复使用。
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
。