Create your own template
Introduction
If you have some code, that will most likely repeat in several different pages, you might want to make a template from it. You want to make it as convenient as possible, that's why it's important to have constistant templates, that you can easily use just by looking at its code.
Here will be explained creation of 3 files:
Main directory
The very first thing you want to do is create a directory for template in /Templates
.xaml template
First of all, template creation is a bit different from page creation. Two tags are needed as the root:
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="|PathToNamespace|">
<ControlTemplate x:Key="|TemplateName|">
<!-- Content will be here -->
</ControlTemplate>
</ResourceDictionary>Next, put your page content in it and define changeable content blocks using this:
For example you might get this:
.xaml.cs file
As the root tag is <ResourceDictionary> , we can't make a class, that inherits from <ContentPage> . For that reason there's nothing hard here, just make a dummy class for .xaml:
TemplateDictionary.xaml
You might've noticed, that there is one file, that isn't in any directory, when you created a directory in /Templates. That's not a mistake, it is a special file, that has a list of all templates in it.
Now you have to add your newly created template, so after that we will continue to creating class for the template.
In file TemplateDictionary.xaml, add namespace of your template and put template in there:
Here it is added as xmlns:pagetemplate and then Page class used as tag.
.cs file
Moving on to the hardest part: creating class, that will manage our template.
Firstly, make namespace and class:
Next, to connect the class with template, set ControlTemplate in class' constructor:
Now, you have to add BindableProperty and configure it for each content block. But first, add alias for Microsoft.Maui.Controls.View, because .net MAUI requires to have View directory, that overwrites it with directory's namespace:
Now you can add variable for content and BindableProperty for each content block like this:
Which leaves us with the following code:
Now you can add all methods needed and everything needed in constructor, but make sure for ControlTemplate = ... to be the first in constructor.
And with this, finished code looks like this:
Now you can use your very own template - Using already existing templates
Last updated