Xamarin XAML语言教程构建ControlTemplate控件模板
来源:互联网 发布:mysql默认事务隔离级别 编辑:程序博客网 时间:2024/06/06 00:10
Xamarin XAML语言教程构建ControlTemplate控件模板
控件模板ControlTemplate
ControlTemplate是从Xamarin.Forms 2.1.0开始被引入的。ControlTemplate被称为控件模板,它将页面的外观和内容进行了分离,从而让开发者可以更方便的创建基于主题的页面。本节将讲解控件模板相关的内容,其中包括构建控件模板以及控件模板的模板绑定等内容。
构建控件模板
控件模板可以在应用程序级别中构建,也可以在页面级别中构建。以下将对这两个构建方式进行讲解。
1.应用程序级别构建
如果开发者要在应用程序级别构建控件模板,首先必须将ResourceDictionary添加到App类中,然后在ResourceDictionary中实现模板的构建。其语法形式如下:
- <Application>
- <Application.Resources>
- <ResourceDictionary>
- <ControlTemplate x:Key="KeyName">
- ……
- </ControlTemplate>
- </ResourceDictionary>
- </Application.Resources>
- </Application>
其中,KeyName指定一个字典键,用来指代控件模板。
构建好模板后,我们需要将这个模板控件显示出来,此时就需要可以模板化的视图。在这些视图中都会存在一个ControlTemplate属性。将此属性设置为创建的控件模板后,控件模板就可以进行显示了。在Xamarin.Forms目前只有4个视图包含ControlTemplate属性,这4个视图如下:
- ContentPage:内容页面
- ContentView:内容视图
- TemplatedPage:模板页面
- TemplatedView:模板视图
【示例14-3:ControlTemplateDemo】下面将在应用程序级别中构建控件模板,实现应用程序主题的切换。具体的操作步骤如下:
(1)打开App.xaml文件,编写代码,实现在应用程序级别中构建控件模板,代码如下:
- <?xml version="1.0" encoding="utf-8" ?>
- <Application xmlns="http://xamarin.com/schemas/2014/forms"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- x:Class="ControlTemplateDemo.App">
- <Application.Resources>
- <ResourceDictionary>
- <!--构建控件模板-->
- <ControlTemplate x:Key="TealTemplate">
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition Height="0.1*" />
- <RowDefinition Height="0.8*" />
- <RowDefinition Height="0.1*" />
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="0.05*" />
- <ColumnDefinition Width="0.95*" />
- </Grid.ColumnDefinitions>
- <BoxView Grid.ColumnSpan="2"
- Color="Teal" />
- <Label Grid.Column="1"
- Text="Knowledge is power."
- TextColor="White"
- FontSize="18"
- VerticalOptions="Center" />
- <ContentPresenter Grid.Row="1"
- Grid.ColumnSpan="2" />
- <BoxView Grid.Row="2"
- Grid.ColumnSpan="2"
- Color="Teal" />
- <Label Grid.Row="2"
- Grid.Column="1"
- Text="Xamarin.Froms XAML"
- TextColor="White"
- FontSize="18"
- VerticalOptions="Center" />
- </Grid>
- </ControlTemplate>
- <!--构建控件模板-->
- <ControlTemplate x:Key="AquaTemplate">
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition Height="0.1*" />
- <RowDefinition Height="0.8*" />
- <RowDefinition Height="0.1*" />
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="0.05*" />
- <ColumnDefinition Width="0.95*" />
- </Grid.ColumnDefinitions>
- <BoxView Grid.ColumnSpan="2"
- Color="Aqua" />
- <Label Grid.Column="1"
- Text="Knowledge is power."
- TextColor="Blue"
- FontSize="18"
- VerticalOptions="Center" />
- <ContentPresenter Grid.Row="1"
- Grid.ColumnSpan="2" />
- <BoxView Grid.Row="2"
- Grid.ColumnSpan="2"
- Color="Aqua" />
- <Label Grid.Row="2"
- Grid.Column="1"
- Text="Xamarin.Froms XAML"
- TextColor="Blue"
- FontSize="18"
- VerticalOptions="Center" />
- </Grid>
- </ControlTemplate>
- </ResourceDictionary>
- </Application.Resources>
- </Application>
在此代码中,我们构建了两个控件模板,一个为TealTemplate控件模板,另一为AquaTemplate控件模板。
阅读全文
0 0
- Xamarin XAML语言教程构建ControlTemplate控件模板
- Xamarin XAML语言教程构建ControlTemplate控件模板 (二)
- Xamarin XAML语言教程构建ControlTemplate控件模板 (三)
- Xamarin XAML语言教程构建ControlTemplate控件模板 (四)
- Xamarin XAML语言教程控件模板的模板绑定
- Xamarin XAML语言教程构建进度条ProgressBar
- Xamarin XAML语言教程模板页面TemplatedPage
- Xamarin XAML语言教程Xamarin.Forms中构建进度条
- Xamarin XAML语言教程模板视图TemplatedView(一)
- Xamarin XAML语言教程模板视图TemplatedView(二)
- Xamarin XAML语言教程使用Xamarin Studio创建XAML(二)
- Xamarin XAML语言教程XAML文件结构与解析XAML
- Xamarin XAML语言教程基本视图ContentView
- Xamarin XAML语言教程使用Visual Studio创建XAML
- Xamarin XAML语言教程Visual Studio中实现XAML预览
- Xamarin XAML语言教程基础语法篇大学霸
- Xamarin XAML语言教程对象元素的声明方式
- Xamarin XAML语言教程Progress属性设置进度条进度
- Matlab基于腐蚀和膨胀的边缘检测
- win10服务主机服务占用大量网络宽带
- SVM基本概要与sklearn.svm.svc 参数说明
- 采云间DPC迁移方案实施计划
- 常见的前端面试题(含答案)
- Xamarin XAML语言教程构建ControlTemplate控件模板
- java各种IO读取文件对比
- Error:Execution failed for task ':wls:processC00DebugManifest'. > Manifest merger failed with multip
- ubuntu下找不到mkinitrd命令的问题
- TCP通信socket缓存读取阻塞
- 索引失效原因总结
- 欢迎使用CSDN-markdown编辑器
- 提取斗鱼直播源的图文教程
- jupyter notebook 配置多anaconda/python不同版本 可以轻松切换python内核