Xamarin.Forms 用户界面——控件——Style——隐式样式
来源:互联网 发布:天天网络教育孙振远 编辑:程序博客网 时间:2024/05/21 05:19
隐式样式
使用隐式样式来自定义控件的外观
- PDF用于离线使用
- 下载PDF
- 示例代码:
- 基本款式
- 使用样式
- 相关文章:
- XAML标记扩展
- 相关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
覆盖到不同的值。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
。
- Xamarin.Forms 用户界面——控件——Style——隐式样式
- Xamarin.Forms 用户界面——控件
- Xamarin.Forms 用户界面——控件——Style——介绍
- Xamarin.Forms 用户界面——控件——Style——显示样式
- Xamarin.Forms 用户界面——控件——Style——全局样式
- Xamarin.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
- Xamarin.Forms 用户界面——控件——Text
- Xamarin.Forms 用户界面——控件——WebView
- spring只扫描指定文件注解
- hdu 5583
- linux初学者-DNS配置篇
- How to install python packages
- <C语言>带参数的主函数理解int main(int argc, char *argv[])
- Xamarin.Forms 用户界面——控件——Style——隐式样式
- VMware虚拟机删除已经安装的centos操作系统
- Google/LintCode:M-摊平嵌套的列表
- 【GDOI2018模拟8.7】最长公共子序列
- CNTK API文档翻译(15)——自然语言理解
- codeblocks下载安装与解决codeblocks找不到编译器的方法
- B
- 赛马
- Rap 安装和配置