ITPub博客

首页 > 应用开发 > IT综合 > Xamarin XAML语言教程构建ControlTemplate控件模板 (三)

Xamarin XAML语言教程构建ControlTemplate控件模板 (三)

原创 IT综合 作者:大学霸 时间:2017-07-18 13:19:22 0 删除 编辑

Xamarin XAML语言教程构建ControlTemplate控件模板 (三)

(3)打开MainPage.xaml.cs文件,编写代码,实现主题的切换功能。代码如下:


  • using System;
  • using System.Collections.Generic;
  • using System.Linq;
  • using System.Text;
  • using System.Threading.Tasks;
  • using Xamarin.Forms;
  • namespace ControlTemplateDemo
  • {
  •     public partial class MainPage : ContentPage
  •     {
  •         bool originalTemplate = true;
  •         ControlTemplate tealTemplate;
  •         ControlTemplate aquaTemplate;
  •         public MainPage()
  •         {
  •             InitializeComponent();
  •             //实例化控件模板
  •             tealTemplate = (ControlTemplate)Application.Current.Resources["TealTemplate"];
  •             aquaTemplate = (ControlTemplate)Application.Current.Resources["AquaTemplate"];
  •         }
  •         //实现模板的切换
  •         void OnButtonClicked(object sender, EventArgs e)
  •         {
  •             originalTemplate = !originalTemplate;
  •             contentView.ControlTemplate = (originalTemplate) ? tealTemplate : aquaTemplate;
  •         }
  •     }
  • }


此时运行程序,会看到如图14.12~14.14所示的效果。当开发者轻拍“改变主题”按钮后,会看到如图14.15~14.17所示的效果。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29597077/viewspace-2142225/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-04-09

  • 博文量
    1047
  • 访问量
    990418