Getting Started with .NET MAUI Switch

13 Nov 20245 minutes to read

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

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

Prerequisites

Before proceeding, ensure the following are set up:

  1. Install .NET 8 SDK or later is installed.
  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 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 Buttons NuGet Package

  1. In Solution Explorer, right-click the project and choose Manage NuGet Packages.
  2. Search for Syncfusion.Maui.Buttons 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 SwitchGettingStarted
    {
        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: Adding the .NET MAUI Switch

    1. To initialize the control, import the Buttons namespace into your code.
    2. Initialize SfSwitch.
    <ContentPage
        . . .    
        xmlns:buttons="clr-namespace:Syncfusion.Maui.Buttons;assembly=Syncfusion.Maui.Buttons">
        <buttons:SfSwitch x:Name="sfSwitch" />
    </ContentPage>
    using Syncfusion.Maui.Buttons;
        namespace SwitchGettingStarted
        {
            public partial class MainPage : ContentPage
            {
                public MainPage()
                {
                    InitializeComponent();           
                    SfSwitch sfSwitch = new SfSwitch();
                    this.Content= sfSwitch;
                }
            }   
        }

    SfSwitch

    Performing an action based on state

    You can switch between states in the .NET MAUI Switch control. When the state changes,the StateChanging and StateChanged events are triggered. These events enable you to perform specific actions depending on the current state of the switch. The StateChanging event also allows you to cancel the transition to a new state if needed.

    The following code example displays a message box when the .NET MAUI Switch is switched to the off state after the work is completed.

    <buttons:SfSwitch x:Name="sfSwitch" StateChanged="SfSwitch_StateChanged"/>
    SfSwitch sfSwitch = new SfSwitch();
    sfSwitch.StateChanged+= SfSwitch_StateChanged;
    this.Content = sfSwitch;
    private void SfSwitch_StateChanged(object sender, SwitchStateChangedEventArgs e)
    {
         DisplayAlert("Message", "SUCCESS", "OK");
    }

    NOTE

    Find the complete getting started sample of the .NET MAUI Switch from this link.

    NOTE

    You can refer to our .NET MAUI Switch feature tour page for its groundbreaking feature representations. You can also explore our .NET MAUI Switch Example that shows you how to render the Switch in .NET MAUI.