Getting Started with .NET MAUI Switch (SfSwitch)
19 Dec 20233 minutes to read
This section explains the steps to configure the .NET MAUI Switch control in a real-time scenario and provides a walk-through on some of the customization features available in the control.
Adding a .NET MAUI Switch reference
The Syncfusion .NET MAUI controls are available in Nuget.org. To add .NET MAUI Switch
to your project, open the NuGet package manager in Visual Studio, search for Syncfusion.Maui.Buttons
, and install it.
Handler registration
In the MauiProgram.cs file, register the handler for the Syncfusion core.
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 SwitchSample
{
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();
}
}
}
Adding the .NET MAUI Switch control
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:syncfusion="clr-namespace:Syncfusion.Maui.Buttons;assembly=Syncfusion.Maui.Buttons"
using Syncfusion.Maui.Buttons;
Initialize Switch
Now, add the SfSwitch
control with a required optimal name using the included namespace.
<syncfusion:SfSwitch />
SfSwitch sfSwitch = new SfSwitch();
this.Content = sfSwitch;
Performing an action based on state
You can switch between the states. When the state is changed the StateChanging
and StateChanged
event will be triggered where you can perform an action based on the current state. The StateChanging
event allows you to cancel moving to a new state.
The following code example displays a message box when switched to off state when work is completed.
<syncfusion: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.