隐式样式
使用隐式样式来自定义控件的外观
隐式样式是同一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
覆盖到不同的值。BackgroundColor
TextColor
Color
在控制级别创建隐式样式
除了在页面级别创建隐式样式之外,还可以在控件级别创建它们,如以下代码示例所示:
<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
。