Articles in this section
Category / Section

How to add events in .NET MAUI Switch (SfSwitch)?

2 mins read

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

Switch2024-02-0813-16-22online-video-cutter.com-ezgif.com-video-to-gif-converter.gif

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!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied