See Also: Switch Members
The following example describes a basic use.
C# Example
using System;
using Xamarin.Forms;
namespace FormsGallery
{
class SwitchDemoPage : ContentPage
{
Label label;
public SwitchDemoPage()
{
Label header = new Label
{
Text = "Switch",
Font = Font.BoldSystemFontOfSize(50),
HorizontalOptions = LayoutOptions.Center
};
Switch switcher = new Switch
{
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.CenterAndExpand
};
switcher.Toggled += switcher_Toggled;
label = new Label
{
Text = "Switch is now False",
Font = Font.SystemFontOfSize(NamedSize.Large),
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.CenterAndExpand
};
// Accomodate iPhone status bar.
this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5);
// Build the page.
this.Content = new StackLayout
{
Children =
{
header,
switcher,
label
}
};
}
void switcher_Toggled(object sender, ToggledEventArgs e)
{
label.Text = String.Format("Switch is now {0}", e.Value);
}
}
}
The Xamarin.Forms.Switch class has the following XAML properties:
| Property | Value |
|---|---|
| IsToggled | true or false, to indicate whether the switch has been toggled. |
| Toggled | The name of an event handler. Note that this tag must appear below IsToggled. |