Shaun Hevey
Creating a calendar control in Xamarin Forms (Part 1)
February 13, 2021
By Shaun Hevey

Xamarin.Forms has a number of versatile controls included in the box to help you help build your own cross platform controls without having to touch any platform specific codes. In this post I will show you how to create a calendar view with controls built in to Xamarin.Forms, you can see the final control below. This control highlights the current day, allows a user to select a day and change the month to any month in the past or the future. The final code is available in this GitHub repo

A mobile calendar control

Note: I am using Visual Studio for Mac to create this control but you should be able to use any IDE that supports Xamarin.Forms to follow along.

Create a Xamarin.Forms application

First create a Xamarin.Forms shell application, with the new wizard window in Visual Studio for Mac shown click new project. Then under Multiplatform > App, then choose Shell Forms App (You can see this in the image below). Then follow the rest of the wizard by filling out the App Name, organisation and target platforms etc.

Visual Studio for Mac new application wizard

Clean up folder structure

Once the default application is created you can delete the following folders

The following folders will be needed but you can delete all the files in them

Updating NuGet packages

Next lets update the built in NuGet packages (make sure you update all projects)

Now add the following NuGet packages that will be required (make sure you update all projects)

Setup Default Page

The last thing to do to setup the project before we get in to creating the calendar control is to create a page that you will create the control in and set it up in Shell to navigate to it.

First create a new Forms ContentPage Xaml in the Views folder and name it CalendarPage.

Visual Studio for Mac new page wizard

Now it is time to update AppShell.xaml to navigate to the calendar page, open AppShell.xaml and remove all <Tabbar> elements at time of writing here is the code I am removing.

<TabBar>
<ShellContent Title="About" Icon="icon_about.png" Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" />
<ShellContent Title="Browse" Icon="icon_feed.png" ContentTemplate="{DataTemplate local:ItemsPage}" />
</TabBar>

<!--
If you would like to navigate to this content you can do so by calling
await Shell.Current.GoToAsync("//LoginPage");
-->
<TabBar>
<ShellContent Route="LoginPage" ContentTemplate="{DataTemplate local:LoginPage}" />
</TabBar>

This is being replaced with a <FlyoutItem> element, this will instruct Shell to create a slide in menu on the left with a link to the page, to CalendarPage that was just created.

    <FlyoutItem Title="Calendar Page" Icon="icon_feed.png">
<Tab>
<ShellContent ContentTemplate="{DataTemplate local:CalendarPage}" />
</Tab>
</FlyoutItem>

Fixing errors

Now there is a few errors you will need to fix before you can build the application if you have followed this guide, at the time of writing these will include.

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="CalendarControl.Views.CalendarPage"
Background="{AppThemeBinding Dark=Black, Light=White}">
<ContentPage.Content>
<StackLayout>
<Label Text="Hello World!" />
</StackLayout>
</ContentPage.Content>
</ContentPage>

You should be able to run the application now and see your calendar page, if you didn't use my code above you might need to set the background colour with the AppThemeBinding line above if your device is set to Dark mode. You can read about this in my post I written on AppThemeBinding

Mobile application with black background and white text Hello World!

This is the end of part 1 of creating this control, you can find the finished code in the GitHub repo under the following Tag CalendarControl-NewProject.

Part 2 is now available, please check it out.