Getting started with .NET MAUI Time Picker (SfTimePicker)

7 Dec 20236 minutes to read

This section explains how to add the Time Picker control. It covers only the basic features needed to get started with the Syncfusion Time Picker.

Creating an application using the .NET MAUI Time Picker

  1. Create a new .NET MAUI application in Visual Studio.

  2. Syncfusion .NET MAUI components are available on nuget.org. To add SfTimePicker to your project, open the NuGet package manager in Visual Studio, search for Syncfusion.Maui.Picker, then install it.

  3. To initialize the control, import the control namespace Syncfusion.Maui.Picker in XAML or C# code.

  4. Initialize SfTimePicker.

<ContentPage   
    . . .
    xmlns:picker="clr-namespace:Syncfusion.Maui.Picker;assembly=Syncfusion.Maui.Picker">

    <picker:SfTimePicker />
</ContentPage>
using Syncfusion.Maui.Picker;
. . .

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        SfTimePicker picker = new SfTimePicker();
        this.Content = picker;
    }
}

Register the handler

The Syncfusion.Maui.Core NuGet is a dependent package for all Syncfusion controls of .NET MAUI. In the MauiProgram.cs file, register the handler for Syncfusion core.

using Syncfusion.Maui.Core.Hosting;
namespace GettingStarted
{
    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();

            builder.ConfigureSyncfusionCore();
            builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("Segoe-mdl2.ttf", "SegoeMDL2");
            });

            return builder.Build();
        }
    }
}

Set header to the Time Picker

The SfTimePicker control allows you to add the header text by setting the Text property in the PickerHeaderView. Enable the header view by setting the Height property in the PickerHeaderView.

<picker:SfTimePicker x:Name="picker">
    <picker:SfTimePicker.HeaderView>
        <picker:PickerHeaderView Text="Time Picker" Height="40" />
    </picker:SfTimePicker.HeaderView>
</picker:SfTimePicker>
SfTimePicker picker = new SfTimePicker();
picker.HeaderView = new PickerHeaderView()
{
    Text = "Time Picker",
    Height = 40,
};

this.Content = picker;

Set header view in .NET MAUI Time picker.

In the SfTimePicker control, validation buttons (OK and Cancel) can be customized by setting the OkButtonText and CancelButtonText properties in the PickerFooterView. It allows you to confirm or cancel the selected time. The OkButtonText can be enabled using the ShowOkButton property in the PickerFooterView.

<picker:SfTimePicker x:Name="Picker">
    <picker:SfTimePicker.FooterView>
        <picker:PickerFooterView ShowOkButton="True" Height="40" />
    </picker:SfTimePicker.FooterView>
</picker:SfTimePicker>
SfTimePicker picker = new SfTimePicker();
picker.FooterView= new PickerFooterView()
{  
    ShowOkButton = true,
    Height = 40,
};

this.Content = picker;

Set footer view in .NET MAUI Time picker.

Set height and width to the Time Picker

The SfTimePicker control allows you to change the height and width by using the [HeightRequest] and [WidthRequest] properties in the SfTimePicker.

<picker:SfTimePicker x:Name="Picker" 
                    HeightRequest="280" 
                    WidthRequest="300">
</picker:SfTimePicker>
SfTimePicker picker = new SfTimePicker()
{
    HeightRequest = 280,
    WidthRequest = 300,
};

this.Content = picker;

Set height and width in .NET MAUI Time picker.

Set selected time to the Time Picker

The SfTimePicker control allows you to select the time using the SelectedTime property in the SfTimePicker. The default value of the SelectedTime is the current time.

<picker:SfTimePicker x:Name="Picker" 
                     SelectedTime="07:22:01">
</picker:SfTimePicker>
SfTimePicker picker = new SfTimePicker()
{
    SelectedTime = new TimeSpan(07, 22, 01),
};

this.Content = picker;

Set Selected time in .NET MAUI Time picker.