CalendarView In Xamarin.Forms

Introduction

In this tutorial, we will learn how to use CalendarView in Xamarin.Forms. This plugin will help more when we are creating a mobile app for the attendance management system.
This Calendar Control is an awesome plugin created by Rebecca and is open-sourced on GitHub. This control offers
  • single date selection,
  • multi-date selection,
  • special date selection and date background selection as image and background pattern.
To know more about it, we will skip into the coding part without delay.

Steps

I have split this part into 3 steps as in the following.
Step 1
Creating new Xamarin.Forms projects.
Step 2
Setting up the plugin for Xamarin.Forms application.
Step 3
Implementing CalendarView in Xamarin.Forms.
Step 1 - Creating new Xamarin.Forms Projects
  • Create a new project by selecting "New Project". Select Xamarin cross-platform app and click OK.

    CalendarView In Xamarin.Forms

  • Then, select Android, iOS, and UWP platforms as shown below with the project template as "Blank" and click OK.

    CalendarView In Xamarin.Forms
Step 2 - Setting up the plugin for Xamarin.Forms Application
  • Open NuGet Package Manager against the solution and do a search for Calendar Control plugin.
  • Click "Install" to install this plugin against your PCL project or .NET Standard Project.
  • We need to install this plugin in all client projects.

    CalendarView In Xamarin.Forms
Step 3 - Implementing CalendarView in Xamarin.Forms
  • After installing the plugin into the project, open your designer page (in my case “MainPage.xaml”) and add the following line for importing the calendar control.
    1. xmlns:controls="clr-namespace:XamForms.Controls;assembly=XamForms.Controls.Calendar"
  • Then, add the calendar control in your page like below.
    1. <controls:Calendar Padding="10,0,10,0"
    2. SelectedBorderWidth="4"
    3. DisabledBorderColor="Black"
    4. ShowNumberOfWeek="false"
    5. StartDay="Monday"
    6. TitleLabelTextColor="Purple"
    7. TitleLeftArrowTextColor="Blue"
    8. SelectedDate="{Binding Date}"
    9. SpecialDates="{Binding Attendances}"
    10. DateCommand="{Binding DateChosen}"/>
  • Here, SelectedDate is used to select date by default, SpecialDates is used to highlight the sepecial dates like "Holidays", "Festivals", "Appointments" and more.

  • In this project, we are using MVVM pattern. So, we should create a page model and add this to binding the context. After that, add bindable properties for SelectedDate, SpecialDates.

  • Selection of a particular date can be identified by the "DateCommand". Below you can find the whole code implementations of the PageModel.
    1. namespace CalendarSample
    2. {
    3. public class MainPageModel : BindableObject
    4. {
    5. private DateTime? _date;
    6. public DateTime? Date
    7. {
    8. get
    9. {
    10. return _date;
    11. }
    12. set
    13. {
    14. _date = value;
    15. OnPropertyChanged(nameof(Date));
    16. }
    17. }
    18. private ObservableCollection<XamForms.Controls.SpecialDate> attendances;
    19. public ObservableCollection<XamForms.Controls.SpecialDate> Attendances
    20. {
    21. get
    22. {
    23. return attendances;
    24. }
    25. set
    26. {
    27. attendances = value;
    28. OnPropertyChanged(nameof(Attendances));
    29. }
    30. }
    31. public Command DateChosen
    32. {
    33. get
    34. {
    35. return new Command((obj) =>
    36. {
    37. System.Diagnostics.Debug.WriteLine(obj as DateTime?);
    38. });
    39. }
    40. }
    41. }
    42. }
  • You can find the ouput of this application below,

    CalendarView In Xamarin.Forms

Full Code

MainPage.xaml
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. xmlns:d="http://xamarin.com/schemas/2014/forms/design"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:controls="clr-namespace:XamForms.Controls;assembly=XamForms.Controls.Calendar"
  7. xmlns:viewmodels="clr-namespace:CalendarSample"
  8. mc:Ignorable="d"
  9. x:DataType="viewmodels:MainPageModel"
  10. x:Class="CalendarSample.MainPage">
  11. <StackLayout>
  12. <controls:Calendar Padding="10,0,10,0"
  13. SelectedBorderWidth="4"
  14. DisabledBorderColor="Black"
  15. ShowNumberOfWeek="false"
  16. StartDay="Monday"
  17. TitleLabelTextColor="Purple"
  18. TitleLeftArrowTextColor="Blue"
  19. SelectedDate="{Binding Date}"
  20. SpecialDates="{Binding Attendances}"
  21. DateCommand="{Binding DateChosen}"/>
  22. </StackLayout>
  23. </ContentPage>
MainPageModel.cs
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using Xamarin.Forms;
  4. namespace CalendarSample
  5. {
  6. public class MainPageModel : BindableObject
  7. {
  8. private DateTime? _date;
  9. public DateTime? Date
  10. {
  11. get
  12. {
  13. return _date;
  14. }
  15. set
  16. {
  17. _date = value;
  18. OnPropertyChanged(nameof(Date));
  19. }
  20. }
  21. private ObservableCollection<XamForms.Controls.SpecialDate> attendances;
  22. public ObservableCollection<XamForms.Controls.SpecialDate> Attendances
  23. {
  24. get
  25. {
  26. return attendances;
  27. }
  28. set
  29. {
  30. attendances = value;
  31. OnPropertyChanged(nameof(Attendances));
  32. }
  33. }
  34. public Command DateChosen
  35. {
  36. get
  37. {
  38. return new Command((obj) =>
  39. {
  40. System.Diagnostics.Debug.WriteLine(obj as DateTime?);
  41. });
  42. }
  43. }
  44. }
  45. }
Reference
Download Code
You can download the code from GitHub. If you have doubts, feel free to post a comment. If you like this article and it is useful to you, do like, share the article & star the repository on GitHub.