Xamarin XAML Language Tutorial Building ControlTemplate control Templates (iv)
2. Building a control template at the page level
If the developer wants to build the control template at the page level, you first have to add resourcedictionary to the page, and then implement the template build in ResourceDictionary, with the following syntax:
- <Page>
- <Page.Resources>
- <ResourceDictionary>
- <controltemplate x:key= "KeyName" >
- ......
- </ControlTemplate>
- </ResourceDictionary>
- </Page.Resources>
- </Page>
Where page represents the pages and the child classes of the page. KeyName is used to specify a dictionary key, which refers to the control template.
Example 14-4:controltemplatecontentpage the following will build the control template in the content page. The code is as follows:
- <?xml version= "1.0" encoding= "Utf-8"?>
- <contentpage xmlns= "Http://xamarin.com/schemas/2014/forms"
- xmlns:x= "Http://schemas.microsoft.com/winfx/2009/xaml"
- Xmlns:local= "Clr-namespace:controltemplatecontentpage"
- x:class= "Controltemplatecontentpage.mainpage" >
- <ContentPage.Resources>
- <ResourceDictionary>
- <!--Building Control templates--
- <controltemplate x:key= "Tealtemplate" >
- <stacklayout verticaloptions= "Centerandexpand"
- Spacing= "20"
- Padding= ">"
- <entry placeholder= "Username"/>
- <entry placeholder= "Password"
- ispassword= "True"/>
- <button text= "Click here to Log in"/>
- <contentpresenter/>
- </StackLayout>
- </ControlTemplate>
- </ResourceDictionary>
- </ContentPage.Resources>
- <contentview x:name= "Contentview"
- Padding= "0,20,0,0"
- controltemplate= "{StaticResource tealtemplate}">
- <frame outlinecolor= "Accent" >
- <label text= "Please enter your account number and the corresponding password after confirming the safety of your environment."
- Fontattributes= "Bold"
- Fontsize= "/>"
- </Frame>
- </ContentView>
- </ContentPage>
When you run the program, you see the effect shown in 14.18~14.20.
Xamarin XAML Language Tutorial Building ControlTemplate control Templates (iv)