Xamarin.Forms Guides | Samples

Xamarin.Forms.Switch Class

A View control that provides a toggled value.

See Also: Switch

Syntax

[Xamarin.Forms.RenderWith(typeof(Xamarin.Forms.Platform._SwitchRenderer))]
public class Switch : View, IElementConfiguration<Switch>

Remarks

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",
                FontSize = Device.GetNamedSize (NamedSize.Large, typeof(Label)),
                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",
                FontSize = Device.GetNamedSize (NamedSize.Large, typeof(Label)),
                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);
        }
    }
}

XAML for Xamarin.Forms supports the following properties for the Switch class:

PropertyValue
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.

Requirements

Namespace: Xamarin.Forms
Assembly: Xamarin.Forms.Core (in Xamarin.Forms.Core.dll)
Assembly Versions: 1.0.0.0, 1.1.0.0, 1.2.0.0, 1.3.0.0, 1.4.0.0, 1.5.0.0, 2.0.0.0

The members of Xamarin.Forms.Switch are listed below.

See Also: View

Public Constructors

Creates a new Switch element with default values.

Public Fields

static readonly
IsToggledPropertyBindableProperty. Backing store for the Switch.IsToggled property.

Public Properties

IsToggledBoolean. Gets or sets a Boolean value that indicates whether this Switch element is toggled.

Public Methods

On<T>() : IPlatformElementConfiguration<T, Switch>
Returns the platform-specific instance of this Switch, on which a platform-specific method may be called.

Public Events

ToggledEvent that is raised when this Switch is toggled.