您的位置:首页 > 其它

Xamarin XAML语言教程模板视图TemplatedView(一)

2017-07-28 13:54 387 查看

Xamarin XAML语言教程模板视图TemplatedView(一)

模板视图TemplatedView

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

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

(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控件模板。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: