How to add events in .NET MAUI Switch (SfSwitch)?
This section illustrates how to add events in the .NET MAUI Switch control.
StateChanged Event
The StateChanged event triggers when the value or state of the IsOn property changes, either by tapping the switch button or programmatically setting the IsOn
property. It provides access to the current and previous values through the SwitchStateChangedEventArgs object.
XAML
<syncfusion:SfSwitch StateChanged="SfSwitch_StateChanged"/>
C#
private async void SfSwitch_StateChanged(object sender, SwitchStateChangedEventArgs e)
{
bool? newValue = e.NewValue;
bool? oldValue = e.OldValue?;
await DisplayAlert("Alert", "Switch State Changed", "close");
}
Output
StateChanging Event
The StateChanging event fires when the IsOn
property’s state is about to change. It allows you to cancel the event if necessary, through the SwitchStateChangingEventArgs object.
XAML
<syncfusion:SfSwitch StateChanging="SfSwitch_StateChanging"/>
C#
private void SfSwitch_StateChanging(object sender, SwitchStateChangingEventArgs e)
{
bool? newValue = e.NewValue;
bool? oldValue = e.OldValue;
if (newValue == null)
{
e.Cancel = true;
}
}
Conclusion
Hope you enjoyed learning how to add events in .NET MAUI Switch.
Refer to our .NET MAUI Switch feature tour page to learn about its other groundbreaking feature representations. You can explore our .NET MAUI Switch documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our .NET MAUI Switch and other .NET MAUI components.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!