Getting Started with .NET MAUI Rating

17 Sep 20245 minutes to read

This section explains how to configure a Rating control in a real-time scenario and also provides a walk-through on some of the customization features available in SfRating control.

To quickly get started with the .NET MAUI Rating, 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 RatingSample
    {
        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 Rating

    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:rating="clr-namespace:Syncfusion.Maui.Inputs;assembly=Syncfusion.Maui.Inputs"/>
    using Syncfusion.Maui.Inputs;

    Initialize Rating

    Now, add the SfRating control with a required optimal name using the included namespace.

    <rating:SfRating x:Name="rating" />
    SfRating rating;
    public MainPage()
    {
        InitializeComponent();
        rating = new SfRating();
        this.Content = rating;
    }

    Set Number of Rating Items

    The number of rating items to be displayed can be customized in the SfRating control. Users can create a rating application using 5 items as follows. The ItemCount property is used to define the number of rating items.

    NOTE

    The default value of ItemCount is 5.

    <rating:SfRating ItemCount="5" />
    SfRating rating;
    public MainPage()
    {
        InitializeComponent();
        rating = new SfRating();
        rating.ItemCount = 5;
    }

    Set Value

    The display value can be set in the SfRating control, which is selected among the items. The following code example shows the display value of three with five rating items. The Value property is used to set display value.

    NOTE

    The default value of this property is 0.

    <rating:SfRating Value="3" />
    SfRating rating;
    public MainPage()
    {
        InitializeComponent();
        rating = new SfRating();
        rating.Value = 3;
    }

    Precision

    The SfRating control provides an option to rate the items in full, half, and exact values. This can be set using the Precision property. By default, the precision mode is Standard.

    <rating:SfRating Precision="Standard" />
    SfRating rating;
    public MainPage()
    {
        InitializeComponent();
        rating = new SfRating();
        rating.Precision = Precision.Standard;
    }

    SfRating Getting Started

    The complete Getting Started sample is available in this link.