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/
,如需转载,请注明出处,否则将追究法律责任。