Xamarin Forms 开关Xamarin.Forms Switch

下载示例 下载示例Download Sample Download the sample

Xamarin Switch控件是一个水平切换按钮, 用户可以对其进行操作以在打开和关闭状态之间切换, 这些状态由一个boolean值表示。The Xamarin.Forms Switch control is a horizontal toggle button that can be manipulated by the user to toggle between on and off states, which are represented by a boolean value. 类继承自View SwitchThe Switch class inherits from View.

以下屏幕截图显示了Switch在 iOS 和 Android 上, 控件在其打开关闭切换状态:The following screenshots show a Switch control in its on and off toggle states on iOS and Android:

IOS 和 Android 上的开启和关闭状态的开关屏幕截图Screenshot of Switches in on and off states, on iOS and Android

Switch控件定义了两个属性:The Switch control defines two properties:

  • IsToggled指示是否打开Switch值。 booleanIsToggled is a boolean value that indicates whether the Switch is on.
  • OnColor是一个Color , 它会影响Switch在切换或开启状态下呈现的方式。OnColor is a Color that affects how the Switch is rendered in the toggled, or on, state.
  • ThumbColor``Color是交换机拇指的。ThumbColor is the Color of the switch thumb.

BindableProperty对象支持这些属性, 这Switch意味着可以对其进行样式化并成为数据绑定的目标。These properties are backed by a BindableProperty object, which means the Switch can be styled and be the target of data bindings.

控件定义一个Toggled事件, 该事件在IsToggled属性更改时通过IsToggled用户操作或应用程序设置属性时引发。 SwitchThe Switch control defines a Toggled event that is fired when the IsToggled property changes, either through user manipulation or when an application sets the IsToggled property. 该事件在点击地图时触发。Toggled 事件附带的 ToggledEventArgs 对象具有一个名为 Value 的属性,类型为 boolThe ToggledEventArgs object that accompanies the Toggled event has a single property named Value, of type bool. 触发事件时, Value属性的值将反映IsToggled属性的新值。When the event is fired, the value of the Value property reflects the new value of the IsToggled property.

创建开关Create a Switch

Switch可以在 XAML 中实例化。A Switch can be instantiated in XAML. 可以IsToggled设置其属性以Switch切换。Its IsToggled property can be set to toggle the Switch. 默认情况下, IsToggled属性为falseBy default, the IsToggled property is false. 下面的示例演示如何使用可选Switch IsToggled的属性集在 XAML 中实例化:The following example shows how to instantiate a Switch in XAML with the optional IsToggled property set:

<Switch IsToggled="true"/>

Switch可以在代码中创建:A Switch can also be created in code:

Switch switchControl = new Switch { IsToggled = true };

切换外观Switch appearance

Switch除了继承Switch ThumbColor自类的属性以外, 还可以定义OnColor和属性。 ViewIn addition to the properties that Switch inherits from the View class, Switch also defines OnColor and ThumbColor properties. Color Switch ThumbColor属性可以设置为在其处于开启状态时定义颜色, 并且属性可以设置为定义交换块的。 OnColorThe OnColor property can be set to define the Switch color when it is toggled to its on state, and the ThumbColor property can be set to define the Color of the switch thumb. 下面的示例演示如何Switch在 XAML 中使用以下属性集来实例化:The following example shows how to instantiate a Switch in XAML with these properties set:

<Switch OnColor="Orange"
        ThumbColor="Green" />

Switch在代码中创建时, 还可以设置属性:The properties can also be set when creating a Switch in code:

Switch switch = new Switch { OnColor = Color.Orange, ThumbColor = Color.Green };

下面的屏幕截图显示SwitchOnColorThumbColor属性集的打开关闭切换状态:The following screenshot shows the Switch in its on and off toggle states, with the OnColor and ThumbColor properties set:

IOS 和 Android 上的开启和关闭状态的开关屏幕截图Screenshot of Switches in on and off states, on iOS and Android

响应交换机状态更改Respond to a Switch state change

当属性更改时, 无论是通过用户操作, 还是在应用程序IsToggled设置属性时Toggled , 都将激发该事件。 IsToggledWhen the IsToggled property changes, either through user manipulation or when an application sets the IsToggled property, the Toggled event fires. 可注册此事件的事件处理程序以响应更改:An event handler for this event can be registered to respond to the change:

<Switch Toggled="OnToggled" />

代码隐藏文件包含的处理程序Toggled事件:The code-behind file contains the handler for the Toggled event:

void OnToggled(object sender, ToggledEventArgs e)
{
    // Perform an action after examining e.Value
}

事件sender处理程序Switch中的参数负责激发此事件。The sender argument in the event handler is the Switch responsible for firing this event. 您可以使用sender属性访问该Switch对象, 或区分共享同一Toggled事件处理程序Switch的多个对象。You can use the sender property to access the Switch object, or to distinguish between multiple Switch objects sharing the same Toggled event handler.

还可以在代码中指定事件处理程序:ToggledThe Toggled event handler can also be assigned in code:

Switch switchControl = new Switch {...};
switchControl.Toggled += (sender, e) =>
{
    // Perform an action after examining e.Value
}

数据绑定开关Data bind a Switch

通过使用数据绑定和触发器来响应更改的Switch切换状态, 可以消除事件处理程序。ToggledThe Toggled event handler can be eliminated by using data binding and triggers to respond to a Switch changing toggle states.

<Switch x:Name="styleSwitch" />
<Label Text="Lorem ipsum dolor sit amet, elit rutrum, enim hendrerit augue vitae praesent sed non, lorem aenean quis praesent pede.">
    <Label.Triggers>
        <DataTrigger TargetType="Label"
                     Binding="{Binding Source={x:Reference styleSwitch}, Path=IsToggled}"
                     Value="true">
            <Setter Property="FontAttributes"
                    Value="Italic, Bold" />
            <Setter Property="FontSize"
                    Value="Large" />
        </DataTrigger>
    </Label.Triggers>
</Label>

在此示例中, Label使用DataTrigger中的绑定表达式来监视IsToggled指定styleSwitchSwitch的属性。In this example, the Label uses a binding expression in a DataTrigger to monitor the IsToggled property of the Switch named styleSwitch. 当此属性变为trueFontAttributes , 将FontSize更改的Label和属性。When this property becomes true, the FontAttributes and FontSize properties of the Label are changed. false FontAttributes FontSize当属性返回到时,的Label和属性将重置为它们的初始状态。 IsToggledWhen the IsToggled property returns to false, the FontAttributes and FontSize properties of the Label are reset to their initial state.

有关触发器的信息, 请参阅Xamarin。窗体触发器For information about triggers, see Xamarin.Forms Triggers.

禁用交换机Disable a Switch

应用程序可能进入正在切换的Switch状态, 这是无效操作。An application may enter a state where the Switch being toggled is not a valid operation. 在这种情况下Switch , 可以通过将其IsEnabled属性设置为false来禁用。In such cases, the Switch can be disabled by setting its IsEnabled property to false. 这会阻止用户操作SwitchThis will prevent users from being able to manipulate the Switch.