Getting Started with .NET MAUI Numeric Entry

17 Sep 20246 minutes to read

This section guides you through setting up and configuring a Numeric Entry in your .NET MAUI application. Follow the steps below to add a basic Numeric Entry to your project.

To quickly get started with the .NET MAUI Numeric Entry, watch this video.

Prerequisites

Before proceeding, ensure the following are in place:

  1. Install .NET 7 SDK or later.
  2. Set up a .NET MAUI environment with Visual Studio 2022 (v17.3 or later) or Visual Studio Code. For Visual Studio Code users, ensure that the .NET MAUI workload is installed and configured as described here.

Step 1: Create a New MAUI Project

Visual Studio

  1. Go to File > New > Project and choose the .NET MAUI App template.
  2. Name the project and choose a location. Then, click Next.
  3. Select the .NET framework version and click Create.

Visual Studio Code

  1. Open the Command Palette by pressing Ctrl+Shift+P and type .NET:New Project and press Enter.
  2. Choose the .NET MAUI App template.
  3. Select the project location, type the project name and press Enter.
  4. Then choose Create project

Step 2: Install the Syncfusion MAUI Inputs NuGet Package

  1. In Solution Explorer, right-click the project and choose Manage NuGet Packages.
  2. Search for Syncfusion.Maui.Inputs and install the latest version.
  3. Ensure the necessary dependencies are installed correctly, and the project is restored.

Step 3: Register the handler

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.

  • C#
  • using Microsoft.Maui;
    using Microsoft.Maui.Hosting;
    using Microsoft.Maui.Controls.Compatibility;
    using Microsoft.Maui.Controls.Hosting;
    using Microsoft.Maui.Controls.Xaml;
    using Syncfusion.Maui.Core.Hosting;
    
    namespace MaskedEntrySample
    {
        public static class MauiProgram
        {
            public static MauiApp CreateMauiApp()
            {
                var builder = MauiApp.CreateBuilder();
                builder
                .UseMauiApp<App>()
                .ConfigureSyncfusionCore()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                });
    
                return builder.Build();
            }      
        }
    }

    Step 4: Add a Basic Numeric Entry

    Step 1: Add the NuGet to the project as discussed in the above reference section.

    Step 2: Add the namespace as shown in the following code sample.

    <xmlns:editors="clr-namespace:Syncfusion.Maui.Inputs;assembly=Syncfusion.Maui.Inputs"/>
    using Syncfusion.Maui.Inputs;

    Step 3: Add the SfNumericEntry control with a required optimal name using the included namespace.

    <editors:SfNumericEntry x:Name="numericEntry" />
    SfNumericEntry sfNumericEntry = new SfNumericEntry();

    .NET MAUI NumericEntry Application

    Editing the value

    By default, the NumericEntry control allows you to enter numeric input and restricts the alphabetic input. Once the Enter key is pressed or control focus is lost, the value of the NumericEntry control is validated and updated based on the format applied.

    <editors:SfNumericEntry HorizontalOptions="Center" 
                            VerticalOptions="Center" 
                            CustomFormat="0.000" />
    SfNumericEntry sfNumericEntry = new SfNumericEntry();
    sfNumericEntry.HorizontalOptions = LayoutOptions.Center;
    sfNumericEntry.VerticalOptions = LayoutOptions.Center;
    sfNumericEntry.CustomFormat = "0.000";

    .NET MAUI NumericEntry value editing

    Change number format

    You can change the format in which the value should be displayed using the CustomFormat property. By default, the value of the CustomFormat property is null.

    <editors:SfNumericEntry HorizontalOptions="Center"
                            VerticalOptions="Center"
                            Value="12.5" 
                            CustomFormat="C2" />
    SfNumberBox sfNumberBox = new SfNumberBox();
    sfNumberBox.HorizontalOptions = LayoutOptions.Center;
    sfNumberBox.VerticalOptions = LayoutOptions.Center;
    sfNumberBox.Value = 12.5;
    sfNumberBox.CustomFormat = "C2";

    .NET MAUI NumericEntry value editing

    Accept null value

    By default, the NumericEntry control allows a null value. A null value is assigned when the user clicks the clear button or clears the input. You can disable this by setting the value of the AllowNull property as false. When the value of the AllowNull property is set to false, and the input is cleared, the NumericEntry control returns it to 0.

    <editors:SfNumericEntry HorizontalOptions="Center"
                            VerticalOptions="Center"
                            Value="12315"
                            AllowNull="False" />
    SfNumericEntry sfNumericEntry = new SfNumericEntry();
    sfNumericEntry.Value=12315;
    sfNumericEntry.HorizontalOptions = LayoutOptions.Center;
    sfNumericEntry.VerticalOptions = LayoutOptions.Center;
    sfNumericEntry.AllowNull = false;

    .NET MAUI NumericEntry prevent empty textbox