与UWP中一样,在资源中定义,然后使用键值静态引用。
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ControlTemplatesSample.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<ControlTemplate x:Key="MyTemplate">
<StackLayout>
<Label Text="Header"
BackgroundColor="Purple"
TextColor="White"
VerticalOptions="Start"/>
<ContentPresenter VerticalOptions="FillAndExpand"/>
<Label Text="Footer"
BackgroundColor="Purple"
TextColor="White"
VerticalOptions="End"/>
</StackLayout>
</ControlTemplate>
</ResourceDictionary>
</ContentPage.Resources>
<ContentView ControlTemplate="{StaticResource MyTemplate}" Margin="0,30,0,0">
<Label Text="Hello World!" VerticalOptions="Center" HorizontalOptions="Center"/>
</ContentView>
</ContentPage>
channel9: https://channel9.msdn.com/Shows/XamarinShow/XamarinForms-101-Control-Templates