; public Light() { // 初始状态为关闭 this.isOn = false; } // 不同状态按下开关逻辑 public void toggleSwitch System.out.println("Light is turned ON"); isOn = true; } }}存在问题如果电灯添加新的状态,则需要修改toggleSwitch () { this.isOn = false; // 初始状态为关 this.isDim = false; // 初始不是调光状态 } public void toggleSwitch 定义一个状态接口(或抽象类)// 状态接口interface State { void toggleSwitch(Light light);}2. 为每个具体状态创建实现类// 具体状态类:灯关闭状态class OffState implements State { @Override public void toggleSwitch(
缺点:耦合太紧密,Light发生变化将影响ToggleSwitch。 解决办法一: 将Light作成Abstract,然后具体类继承自Light。 ? 优点:ToggleSwitch依赖于抽象类Light,具有更高的稳定性,而BulbLight与TubeLight继承自Light,可以根据"开放-封闭"原则进行扩展。 只要Light不发生变化,BulbLight与TubeLight的变化就不会波及ToggleSwitch。 缺点:如果用ToggleSwitch控制一台电视就很困难了。
import React, { Component } from "react";import { connect } from "react-redux";import { toggleSwitch /UiReducer"; class Toggle extends Component { render() { const { ui, toggleSwitch } = this.props; 我们要做的就是 import React from "react";import { connect } from "react-redux";import { toggleSwitch } from /UiReducer"; const Toggle = ({ ui, toggleSwitch }) => (
ApplicationPageBackgroundThemeBrush}"> <Grid Background="{ThemeResource SystemBackgroundAltHighBrush}"> <ToggleSwitch HorizontalAlignment="Center" Toggled="ToggleSwitch_OnToggled"></ToggleSwitch> </Grid> </Grid> SystemBackgroundAltHighBrush 是我们两个资源的,其中一个是白天,一个不是 private void ToggleSwitch_OnToggled(object sender, RoutedEventArgs e) { View.Theme
ApplicationPageBackgroundThemeBrush}"> <Grid Background="{ThemeResource SystemBackgroundAltHighBrush}"> <ToggleSwitch HorizontalAlignment="Center" Toggled="ToggleSwitch_OnToggled"></ToggleSwitch> </Grid> </Grid> SystemBackgroundAltHighBrush 是我们两个资源的,其中一个是白天,一个不是 private void ToggleSwitch_OnToggled(object sender, RoutedEventArgs e) { View.Theme
Page Transitions Existing Components GestureService/GestureListener ContextMenu DatePicker TimePicker ToggleSwitch application AutoCompleteBox ContextMenu DatePicker and TimePicker Gestures ListPicker LongListSelector ToggleSwitch
= {false:'关',true:'开'} this.state = { switch1Value: true, } } toggleSwitch View style = {styles.container}> <Switch onValueChange = {this.toggleSwitch = {false:'关',true:'开'} this.state = { switch1Value: true, } } toggleSwitch trackColor={{false:"#eeeeee",true:"#999999"}} onValueChange = {this.toggleSwitch
使用 ToggleSwitch 控件让用户在打开和关闭状态之间切换选项。使用IsOn属性来确定开关的状态。 例如电灯开关。
/** 添加系统托盘 * */ let toggleSwitch = true; let toggleFlag = false; let timer const icon1 = path.join tray.on('right-click', () => { const menuConfig = Menu.buildFromTemplate([ { label: toggleSwitch '开启闪烁图标' : '关闭闪烁图标', click: () => { if (toggleSwitch) { timer = setInterval } else { tray.setImage(icon1) clearInterval(timer) } toggleSwitch toggleSwitch } }, { label: '退出', click: () => app.quit() }
Style.Triggers> </Style> 2.4 ControlTemplate vs DataTemplate 模板类型 用途 典型场景 ControlTemplate 重绘控件外观 自定义 ToggleSwitch
AutoCompleteBox、ListPicker、LongListSelector、Page Transitions、GestureListener、 ContextMenu、DatePicker、 TimePicker、 ToggleSwitch
</TextBlock.Text> </TextBlock> <ToggleSwitch Header="play automatically" />
│ ├── refineddns_main.ps1 │ │ └── runner_.vbs │ ├── Read\ me.txt │ ├── panel │ │ ├── ToggleSwitch.dll
// 开关特殊处理 if (container.classList.contains('switch')) { this.toggleSwitch }); }); } // 切换开关状态 toggleSwitch
"#f5dd4b" : "#f4f3f4"} ios_backgroundColor="#3e3e3e" onValueChange={toggleSwitch}