Xamarin Forms – Custom Visual State triggers and Control Templates

Not so long ago there was this little online event called Xamarin Expert Day that featured a bunch of awesome speakers.
Now one talk got my full attention, it was the one from Hussain Abbasi about Supercharge You Custom Controls With Control Templates ( rewatch it here… ).

When I developed apps uniquely for Windows with UWP, I used Control Templates often. But with Xamarin Forms I never took the time to get much into it… Hussain did a great job explaining the principals on how and why to use them and with that it spiked my interest again to try something out with Xamarin Forms.
The fact I did not yet used them often, is because the Visual State Manager in Xamarin Forms is still not up to par with the one we have in UWP. But since the VSM evolved a bit in Xamarin Forms, I thought the time was ripe to try something with it again.

So one example Hussain highlighted in his talk, was how you could show a different view of some data by swapping the style of a ContentView while using Control Templates. He did this in code while using Xamarin Essentials to detect landscape or portrait mode of your phone.
Let me show you another way of doing a similar thing, but this time all declared from markup XAML.
This is not 100% true, because this whole process will only work because we first need to create our own StateTrigger in code, that we can use with the VSM, but using it is done all from XAML.

The setup of our example is as follows. You have a certain data structure that you need to present to the user, but each data element is of a certain category. In our case we are using Animal types ( Cat, Dog, Bird ).
If we want to have a different view depending on the animal type, we can leverage control templates and swap the style of our ContentView based on our data. And because we are going to do it from XAML this should all be done through Data Binding.

Let’s start with the basics, how does our data look like.

It’s a simple class that could hold several data points, most important part for our demo is the AnimalType enum, this categorizes our data.
Currently it consists of 3 types.

Depending on this enum we would like to present the data differently, a control template is a fine choice for this. You can have some layout depending on what type of animal it is and still show your regular content inside of it.
For this to work, we need to create a ContentView that will act as host container for our Control Templates.

There is a lot going on in this host container, so let’s break it down a bit.
First thing to notice here, is that we are only using XAML to handle the different views, we leverage the Visual State Manager for this. It will, depending on the AnimalType enum, select the correct Control Template and apply it as Style to the ContentView!

For the Visual State Manager to be able to know what Control Template to use, we use a Custom State Trigger, we will pass the enum type, 1 target enum value of that type and the actual enum value of the data item to the trigger.
Inside this Custom State Trigger we will validate these parameters and set the trigger active or not if the needed criteria matches. In this case, if the current data item enum value equals the target enum value given in XAML.

Passing the actual enum type is not mandatory, but I wanted to show you how this could be done, so that you can also validate if the enum values are indeed contained in the given enum type.
Creating your own Custom State Triggers is not that difficult, you only need to inherit from StateTriggerBase and call the SetActive() method with true or false if your criteria is reached, inside the UpdateTrigger.

The other thing to notice in the Visual State Manager code in the ContentView above, was the fact we can actually swap the way the ContentView is composed by applying a certain style!
Each style is a Control Template on his own, where we can use the ContentPresenter tag to embed the XAML layout we defined in our CollectionView for our list item. Each ControlTemplate in this example, almost looks the same, I only changed the background color and the category image.

Only thing left out, is how we now use the Content View container host in our list… in our MainPage we define a CollectionView, assign a List of AnimalModel items and use data binding to pass along the data points.

The definition of the actual styles is done in the App.xaml, we set these as StaticResources in the global resource dictionary.

And with that, you’ll have following layout:

I know this was a longer post than usual, but it’s because several different components are in play here!
The potential they have is huge, so I would encourage you to try them out.

As always the actual example can be found on my GitHub here…