溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Xamarin XAML語言中如何實現(xiàn)模板視圖TemplatedView

發(fā)布時間:2021-12-21 13:38:44 來源:億速云 閱讀:140 作者:小新 欄目:互聯(lián)網(wǎng)科技

這篇文章給大家分享的是有關(guān)Xamarin XAML語言中如何實現(xiàn)模板視圖TemplatedView的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

模板視圖TemplatedView

與模板頁面相對的是TemplatedView,它被稱為模板視圖,它的功能和模板頁面類似,也是用來顯示控件模板的,只不過比模板頁面更加靈活。TemplatedView提供ControlTemplate屬性,實現(xiàn)對控件模板的關(guān)聯(lián),從而展現(xiàn)對應(yīng)的界面。

【示例14-6TemplatedViewDemo】以下將使用模板視圖顯示控件模板,并實現(xiàn)模板的切換。具體的操作步驟如下:

1)打開App.xaml文件,編寫代碼,實現(xiàn)在應(yīng)用程序級別中構(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="TemplatedViewDemo.App">

  •   <Application.Resources>

  • <ResourceDictionary>

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

  •       <ControlTemplate x:Key="ChineseTemplate">

  •         <StackLayout>

  •           <StackLayout VerticalOptions="End">

  •             <BoxView Color="Aqua" />

  •           </StackLayout>

  •           <StackLayout Spacing="35"

  •             VerticalOptions="CenterAndExpand"  >

  •             <Frame OutlineColor="Accent">

  •               <StackLayout Spacing="20"

  •                            VerticalOptions="CenterAndExpand"

  •                            HorizontalOptions="Center">

  •                 <Label Text="山居秋暝"

  •                        FontSize="30"

  •                        FontAttributes="Bold"

  •                        HorizontalOptions="Center"/>

  •                 <Label Text="空山新雨后,天氣晚來秋。"

  •                        FontSize="18"/>

  •                 <Label Text="明月松間照,清泉石上流。"

  •                        FontSize="18"/>

  •                 <Label Text="竹喧歸浣女,蓮動下漁舟。"

  •                        FontSize="18"/>

  •                 <Label Text="隨意春芳歇,王孫自可留。"

  •                        FontSize="18"/>

  •               </StackLayout>

  •             </Frame>

  •             <Button Command="{TemplateBinding Parent. CommandEnglish}"

  •                       Text="Enter English Template" />

  •           </StackLayout>

  •         </StackLayout>

  •       </ControlTemplate>

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

  •       <ControlTemplate x:Key="EnglishTemplate">

  •         <StackLayout>

  •           <StackLayout VerticalOptions="End">

  •             <BoxView Color="Green" />

  •           </StackLayout>

  •           <StackLayout Spacing="35"

  •                        VerticalOptions="CenterAndExpand"  >

  •             <Frame OutlineColor="Accent">

  •               <Label Text="your life only lasts for a few decades, so be sure that you don\'t leave any regrets. laugh or cry as you like, and it‘s meaningless to oppress yourself."

  •                      FontAttributes="Bold"

  •                      FontSize="18"/>

  •             </Frame>

  •             <Button Command="{TemplateBinding Parent.CommandChinese}"

  •                     Text="Enter Chinese Template" />

  •           </StackLayout>

  •         </StackLayout>

  •       </ControlTemplate>

  •     </ResourceDictionary>

  •   </Application.Resources>

  • </Application>

在此代碼中,我們構(gòu)建了兩個控件模板,一個為ChineseTemplate控件模板,另一為EnglishTemplate控件模板。

感謝各位的閱讀!關(guān)于“Xamarin XAML語言中如何實現(xiàn)模板視圖TemplatedView”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI