Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6542010
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: Android平台

2019-08-06 21:38:28

Switch和CheckBox

应用程序通常需要来自用户的布尔输入,这需要某种方式让用户将程序选项切换为开或关,是或否,真或假,或者您想要考虑它。 在Xamarin.Forms中,这是一个名为Switch的视图。
切换基础知识
Switch只定义了一个属性,名为bool类型的IsToggled,它会触发Toggled事件以指示此属性的更改。 在代码中,您可能倾向于为Switch指定一个开关名称,但这是一个C#关键字,因此您需要选择其他内容。 但是,在XAML中,您可以将x:Name属性设置为切换,并且XAML解析器将巧妙地创建名为@switch的字段,这是C#允许您使用C#关键字定义变量名称的方式。
SwitchDemo程序创建两个带有两个识别标签的Switch元素:“Italic”和“Boldface”。 每个Switch都有自己的事件处理程序,它格式化StackLayout底部的较大Label:



点击(此处)折叠或打开

  1. <ContentPage xmlns=""
  2.              xmlns:x=""
  3.              x:Class="SwitchDemo.SwitchDemoPage">
  4.     <StackLayout Padding="10, 0">
  5.         <StackLayout HorizontalOptions="Center"
  6.                      VerticalOptions="CenterAndExpand">
  7.             <StackLayout Orientation="Horizontal"
  8.                          HorizontalOptions="End">
  9.                 <Label Text="Italic: "
  10.                        VerticalOptions="Center" />
  11.                 <Switch Toggled="OnItalicSwitchToggled"
  12.                         VerticalOptions="Center" />
  13.             </StackLayout>
  14.  
  15.             <StackLayout Orientation="Horizontal"
  16.                          HorizontalOptions="End">
  17.                 <Label Text="Boldface: "
  18.                        VerticalOptions="Center" />
  19.                 <Switch Toggled="OnBoldSwitchToggled"
  20.                         VerticalOptions="Center" />
  21.             </StackLayout>
  22.         </StackLayout>
  23.         <Label x:Name="label"
  24.                Text=
  25. "Just a little passage of some sample text that can be formatted
  26. in italic or boldface by toggling the two Switch elements."
  27.                FontSize="Large"
  28.                HorizontalTextAlignment="Center"
  29.                VerticalOptions="CenterAndExpand" />
  30.  
  31.     </StackLayout>
  32. </ContentPage>

Toggled事件处理程序有第二个参数ToggledEventArgs,它具有类型为bool的Value属性,指示IsToggled属性的新状态。 SwitchDemo中的事件处理程序使用此值来设置或清除long Label的FontAttributes属性中的特定FontAttributes标志:

点击(此处)折叠或打开

  1. public partial class SwitchDemoPage : ContentPage
  2. {
  3.     public SwitchDemoPage()
  4.     {
  5.         InitializeComponent();
  6.     }
  7.     void OnItalicSwitchToggled(object sender, ToggledEventArgs args)
  8.     {
  9.         if (args.Value)
  10.         {
  11.             label.FontAttributes |= FontAttributes.Italic;
  12.         }
  13.         else
  14.         {
  15.             label.FontAttributes &= ~FontAttributes.Italic;
  16.         }
  17.     }
  18.     void OnBoldSwitchToggled(object sender, ToggledEventArgs args)
  19.     {
  20.         if (args.Value)
  21.         {
  22.             label.FontAttributes |= FontAttributes.Bold;
  23.         }
  24.         else
  25.         {
  26.             label.FontAttributes &= ~FontAttributes.Bold;
  27.         }
  28.     }
  29. }
Switch在三个平台上有不同的外观:
2018_09_11_114041
请注意,程序将两个Switch视图对齐,这使其外观更具吸引力,但这也意味着文本标签必然会有些错位。 要完成此格式化,XAML文件会将Label和Switch元素对中的每一个放在水平StackLayout中。每个水平StackLayout的HorizontalOptions设置为End,它对齐右边的每个StackLayout,父StackLayout将屏幕上的标签和开关集合中心设置为HorizontalOptions设置为Center。 在水平StackLayout中,两个视图的VerticalOptions属性都设置为Center。 如果Switch高于Label,则Label相对于Switch垂直居中。 但是如果Label比Switch高,Switch也相对于Label垂直居中。
阅读(998) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~