XamarinXAML語(yǔ)言如何構(gòu)建ControlTemplate控件模板

小編給大家分享一下Xamarin XAML語(yǔ)言如何構(gòu)建ControlTemplate控件模板,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來(lái),是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元鄖西做網(wǎng)站,已為上家服務(wù),為鄖西各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):13518219792

控件模板ControlTemplate

ControlTemplate是從Xamarin.Forms 2.1.0開(kāi)始被引入的。ControlTemplate被稱(chēng)為控件模板,它將頁(yè)面的外觀和內(nèi)容進(jìn)行了分離,從而讓開(kāi)發(fā)者可以更方便的創(chuàng)建基于主題的頁(yè)面。

構(gòu)建控件模板

控件模板可以在應(yīng)用程序級(jí)別中構(gòu)建,也可以在頁(yè)面級(jí)別中構(gòu)建。以下將對(duì)這兩個(gè)構(gòu)建方式進(jìn)行講解。

1.應(yīng)用程序級(jí)別構(gòu)建

如果開(kāi)發(fā)者要在應(yīng)用程序級(jí)別構(gòu)建控件模板,首先必須將ResourceDictionary添加到App類(lèi)中,然后在ResourceDictionary中實(shí)現(xiàn)模板的構(gòu)建。其語(yǔ)法形式如下:

  • <Application>

  • <Application.Resources>

  • <ResourceDictionary>

  • <ControlTemplate x:Key="KeyName">

  • ……

  • </ControlTemplate>

  • </ResourceDictionary>

  • </Application.Resources>

  • </Application>

其中,KeyName指定一個(gè)字典鍵,用來(lái)指代控件模板。

構(gòu)建好模板后,我們需要將這個(gè)模板控件顯示出來(lái),此時(shí)就需要可以模板化的視圖。在這些視圖中都會(huì)存在一個(gè)ControlTemplate屬性。將此屬性設(shè)置為創(chuàng)建的控件模板后,控件模板就可以進(jìn)行顯示了。在Xamarin.Forms目前只有4個(gè)視圖包含ControlTemplate屬性,這4個(gè)視圖如下:

  • ContentPage:內(nèi)容頁(yè)面

  • ContentView:內(nèi)容視圖

  • TemplatedPage:模板頁(yè)面

  • TemplatedView:模板視圖

【示例14-3:ControlTemplateDemo】下面將在應(yīng)用程序級(jí)別中構(gòu)建控件模板,實(shí)現(xiàn)應(yīng)用程序主題的切換。具體的操作步驟如下:

(1)打開(kāi)App.xaml文件,編寫(xiě)代碼,實(shí)現(xiàn)在應(yīng)用程序級(jí)別中構(gòu)建控件模板,代碼如下:

  • <?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>

  • <!--構(gòu)建控件模板-->

  •       <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>

  • <!--構(gòu)建控件模板-->

  •       <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>

在此代碼中,我們構(gòu)建了兩個(gè)控件模板,一個(gè)為T(mén)ealTemplate控件模板,另一為AquaTemplate控件模板。

看完了這篇文章,相信你對(duì)“Xamarin XAML語(yǔ)言如何構(gòu)建ControlTemplate控件模板”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)站名稱(chēng):XamarinXAML語(yǔ)言如何構(gòu)建ControlTemplate控件模板
轉(zhuǎn)載源于:http://bm7419.com/article40/pscceo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)公司網(wǎng)站收錄關(guān)鍵詞優(yōu)化、網(wǎng)站策劃網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)