温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Xamarin XAML语言中如何实现模板视图TemplatedView

发布时间:2021-12-21 13:38:44 来源:亿速云 阅读:134 作者:小新 栏目:互联网科技

这篇文章给大家分享的是有关Xamarin XAML语言中如何实现模板视图TemplatedView的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

模板视图TemplatedView

与模板页面相对的是TemplatedView,它被称为模板视图,它的功能和模板页面类似,也是用来显示控件模板的,只不过比模板页面更加灵活。TemplatedView提供ControlTemplate属性,实现对控件模板的关联,从而展现对应的界面。

【示例14-6TemplatedViewDemo】以下将使用模板视图显示控件模板,并实现模板的切换。具体的操作步骤如下:

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="TemplatedViewDemo.App">

  •   <Application.Resources>

  • <ResourceDictionary>

  • <!--构建控件模板-->

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

  • <!--构建控件模板-->

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

在此代码中,我们构建了两个控件模板,一个为ChineseTemplate控件模板,另一为EnglishTemplate控件模板。

感谢各位的阅读!关于“Xamarin XAML语言中如何实现模板视图TemplatedView”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI