Xamarin XAML Language Tutorial template view Templatedview (ii)
(2) Open the MainPage.xaml file, write the code, and apply the built control template to the Templatedview. The code is as follows:
- <?xml version= "1.0" encoding= "Utf-8"?>
- <contentpage xmlns= "Http://xamarin.com/schemas/2014/forms"
- xmlns:x= "Http://schemas.microsoft.com/winfx/2009/xaml"
- Xmlns:local= "Clr-namespace:templatedviewdemo"
- x:class= "Templatedviewdemo.mainpage" >
- <templatedview x:name= "Templatedview"
- Padding= "0,20,0,0"
- controltemplate= "{StaticResource chinesetemplate}" >
- </TemplatedView>
- </ContentPage>
(3) Open the MainPage.xaml.cs file, write code, and implement the switch function of the control template. The code is as follows:
- Using System;
- Using System.Collections.Generic;
- Using System.Linq;
- Using System.Text;
- Using System.Threading.Tasks;
- Using System.Windows.Input;
- Using Xamarin.Forms;
- Namespace Templatedviewdemo
- {
- public partial class Mainpage:contentpage
- {
- ControlTemplate Chinesetemplate;
- ControlTemplate Englishtemplate;
- Public ICommand commandenglish {private set; get;}
- Public ICommand Commandchinese {private set; get;}
- Public MainPage ()
- {
- Commandenglish = new Command (() = Onenterenglishtemplate ());
- Commandchinese = new Command (() = Onenterchinesetemplate ());
- InitializeComponent ();
- Chinesetemplate = (ControlTemplate) application.current.resources["Chinesetemplate"];
- Englishtemplate = (ControlTemplate) application.current.resources["Englishtemplate"];
- }
- Set as Englishtemplate control template
- Public void Onenterenglishtemplate ()
- {
- Templatedview.controltemplate = englishtemplate;
- }
- Set as Chinesetemplate control template
- public void Onenterchinesetemplate ()
- {
- Templatedview.controltemplate = chinesetemplate;
- }
- }
- }
When you run the program, you see the effect shown in 14.21~14.23. When the developer taps the Enter English template button, you will see the effect shown in 14.24~14.26. When the developer taps the Enter Chinese template button, you'll see the effect shown in 14.21~14.23.
Xamarin XAML Language Tutorial template view Templatedview (ii)