How to construct ControlTemplate control template in Xamarin XAML language to realize theme switching function
Editor to share with you how to build ControlTemplate control template in Xamarin XAML language to achieve theme switching function, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
(3) Open the MainPage.xaml.cs file and write the code to realize the theme switching function. The code is as follows:
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 ()
/ / instantiate the control template
TealTemplate = (ControlTemplate) Application.Current.Resources ["TealTemplate"]
AquaTemplate = (ControlTemplate) Application.Current.Resources ["AquaTemplate"]
}
/ / switch between templates
Void OnButtonClicked (object sender, EventArgs e)
{
OriginalTemplate =! originalTemplate
ContentView.ControlTemplate = (originalTemplate)? TealTemplate: aquaTemplate
}
}
}
When you run the program at this point, you will see the effect shown in figure 14.12-14.14. When the developer taps the "change theme" button, you will see the effect shown in figure 14.15-14.17.
These are all the contents of the article "how to build ControlTemplate control templates in Xamarin XAML language to achieve theme switching". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!