Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6558750
  • 博文数量: 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-07 11:01:40

传统的CheckBox
在更传统的图形环境中,允许用户选择布尔值的用户界面对象称为CheckBox,通常具有一些文本,其中的框可以为空或填充X或复选标记。 CheckBox优于Switch的一个优点是文本标识符是visual的一部分,不需要添加单独的Label。
在Xamarin.Forms中创建自定义视图的一种方法是编写特定于每个平台的渲染器的特殊类,并在每个平台中引用视图。第27章对此进行了论证。
但是,也可以通过组合来自其他视图的视图在Xamarin.Forms中创建自定义视图。首先从ContentView派生一个类,将其Content属性设置为StackLayout(例如),然后在其上添加一个或多个视图。 (您在第8章的ColorView类中看到了这种技术的示例。)您可能还需要定义一个或多个属性,并且可能需要一些事件,但您将希望利用已建立的可绑定基础结构由BindableObject和BindableProperty类。这样可以对属性进行样式设置并成为数据绑定的目标。
CheckBox只包含ContentView上的两个Label元素:一个Label显示与CheckBox关联的texta,另一个显示一个框。 TapGestureRecognizer检测何时点击CheckBox。
CheckBox类已添加到Xamarin.FormsBook.Toolkit库中,该库包含在本书的可下载代码中。以下是您自己做的方法:
在Visual Studio中,您可以从“添加新项”对话框中选择“表单Xaml页”。但是,当您真正想要一个派生自Con?tentView的类时,这将创建一个派生自ContentPage的类。只需将XAML文件的根元素从ContentPage更改为ContentView,并将代码隐藏文件中的基类从ContentPage更改为ContentView。
但是,在Xamarin Studio中,您只需从“新建文件”对话框中选择“表单ContentView Xaml”即可。
这是CheckBox.xaml文件:

点击(此处)折叠或打开

  1. <ContentView xmlns=""
  2.              xmlns:x=""
  3.              x:Class="Xamarin.FormsBook.Toolkit.CheckBox">
  4.     <StackLayout Orientation="Horizontal">
  5.         <Label x:Name="boxLabel" Text="?" />
  6.         <Label x:Name="textLabel" />
  7.     </StackLayout>
  8.     <ContentView.GestureRecognizers>
  9.         <TapGestureRecognizer Tapped="OnCheckBoxTapped" />
  10.     </ContentView.GestureRecognizers>
  11. </ContentView>
那个Unicode字符称为投票箱字符,它只是一个空方块。 字符 u2611是带支票的投票箱,而 u2612是带X的投票箱。为了表示已检查状态,此CheckBox代码隐藏文件将boxLabel的Text属性设置为 u2611(您很快就会看到)。

CheckBox的代码隐藏文件定义了三个属性:

  • Text
  • FontSize
  • IsChecked

CheckBox还定义了一个名为IsCheckedChanged的事件。
CheckBox还应该像Label和But?那样定义FontAttributes和FontFamily属性吗? 也许,但这些额外的属性对于专注于用户的视图并不十分重要。
CheckBox定义的所有三个属性都由可绑定属性支持。 code-be?hind文件创建所有三个BindableProperty对象,属性更改的处理程序在这些方法中定义为lambda函数。
请记住,属性更改的处理程序是静态的,因此需要将第一个参数强制转换为CheckBox对象,以引用类中的实例属性和事件。 IsChecked的属性?更改处理程序负责更改表示已检查和未检查状态的字符并触发IsCheckedChanged事件:


点击(此处)折叠或打开

  1. namespace Xamarin.FormsBook.Toolkit
  2. {
  3.     public partial class CheckBox : ContentView
  4.     {
  5.         public static readonly BindableProperty TextProperty =
  6.             BindableProperty.Create(
  7.                 "Text",
  8.                 typeof(string),
  9.                 typeof(CheckBox),
  10.                 null,
  11.                 propertyChanged: (bindable, oldValue, newValue) =>
  12.                 {
  13.                     ((CheckBox)bindable).textLabel.Text = (string)newValue;
  14.                 });
  15.         public static readonly BindableProperty FontSizeProperty =
  16.             BindableProperty.Create(
  17.                 "FontSize",
  18.                 typeof(double),
  19.                 typeof(CheckBox),
  20.                 Device.GetNamedSize(NamedSize.Default, typeof(Label)),
  21.                 propertyChanged: (bindable, oldValue, newValue) =>
  22.                 {
  23.                     CheckBox checkbox = (CheckBox)bindable;
  24.                     checkbox.boxLabel.FontSize = (double)newValue;
  25.                     checkbox.textLabel.FontSize = (double)newValue;
  26.                 });
  27.  
  28.         public static readonly BindableProperty IsCheckedProperty =
  29.             BindableProperty.Create(
  30.                 "IsChecked",
  31.                 typeof(bool),
  32.                 typeof(CheckBox),
  33.                 false,
  34.                 propertyChanged: (bindable, oldValue, newValue) =>
  35.                 {
  36.                     // Set the graphic.
  37.                     CheckBox checkbox = (CheckBox)bindable;
  38.                     checkbox.boxLabel.Text = (bool)newValue ? "\u2611" : "\u2610";
  39.                     // Fire the event.
  40.                     EventHandler<bool> eventHandler = checkbox.CheckedChanged;
  41.                     if (eventHandler != null)
  42.                     {
  43.                         eventHandler(checkbox, (bool)newValue);
  44.                     }
  45.                 });
  46.  
  47.         public event EventHandler<bool> CheckedChanged;
  48.         public CheckBox()
  49.         {
  50.             InitializeComponent();
  51.         }
  52.         public string Text
  53.         {
  54.             set { SetValue(TextProperty, value); }
  55.             get { return (string)GetValue(TextProperty); }
  56.         }
  57.         [TypeConverter(typeof(FontSizeConverter))]
  58.         public double FontSize
  59.         {
  60.             set { SetValue(FontSizeProperty, value); }
  61.             get { return (double)GetValue(FontSizeProperty); }
  62.         }
  63.         public bool IsChecked
  64.         {
  65.             set { SetValue(IsCheckedProperty, value); }
  66.             get { return (bool)GetValue(IsCheckedProperty); }
  67.         }
  68.         // TapGestureRecognizer handler.
  69.         void OnCheckBoxTapped(object sender, EventArgs args)
  70.         {
  71.             IsChecked = !IsChecked;
  72.         }
  73.     }
  74. }
注意FontSize属性上的TypeConverter。 这允许在XAML中使用诸如“Small”和“Large”之类的属性值设置属性。
TapGestureRecognizer的Tapped处理程序位于类的底部,只需使用C#逻辑否定运算符来填充IsChecked属性。 切换布尔变量的更短语句使用异或赋值运算符:

点击(此处)折叠或打开

  1. IsChecked ^= true;
CheckBoxDemo程序与SwitchDemo程序非常相似,只是标记大大简化,因为CheckBox包含自己的Text属性:

点击(此处)折叠或打开

  1. <ContentPage xmlns=""
  2.              xmlns:x=""
  3.              xmlns:toolkit=
  4.                  "clr-namespace:Xamarin.FormsBook.Toolkit;assembly=Xamarin.FormsBook.Toolkit"
  5.              x:Class="CheckBoxDemo.CheckBoxDemoPage">
  6.     <StackLayout Padding="10, 0">
  7.         <StackLayout HorizontalOptions="Center"
  8.                      VerticalOptions="CenterAndExpand">
  9.  
  10.             <toolkit:CheckBox Text="Italic"
  11.                               FontSize="Large"
  12.                               CheckedChanged="OnItalicCheckBoxChanged" />
  13.             <toolkit:CheckBox Text="Boldface"
  14.                               FontSize="Large"
  15.                               CheckedChanged="OnBoldCheckBoxChanged" />
  16.         </StackLayout>
  17.         <Label x:Name="label"
  18.                Text=
  19. "Just a little passage of some sample text that can be formatted
  20. in italic or boldface by toggling the two custom CheckBox views."
  21.                FontSize="Large"
  22.                HorizontalTextAlignment="Center"
  23.                VerticalOptions="CenterAndExpand" />
  24.     </StackLayout>
  25. </ContentPage>
代码隐藏文件也与早期的程序非常相似:

点击(此处)折叠或打开

  1. public partial class CheckBoxDemoPage : ContentPage
  2. {
  3.     public CheckBoxDemoPage()
  4.     {
  5.         InitializeComponent();
  6.     }
  7.     void OnItalicCheckBoxChanged(object sender, bool isChecked)
  8.     {
  9.         if (isChecked)
  10.         {
  11.             label.FontAttributes |= FontAttributes.Italic;
  12.         }
  13.         else
  14.         {
  15.             label.FontAttributes &= ~FontAttributes.Italic;
  16.         }
  17.     }
  18.     void OnBoldCheckBoxChanged(object sender, bool isChecked)
  19.     {
  20.         if (isChecked)
  21.         {
  22.             label.FontAttributes |= FontAttributes.Bold;
  23.         }
  24.         else
  25.         {
  26.             label.FontAttributes &= ~FontAttributes.Bold;
  27.         }
  28.     }
  29. }
有趣的是,复选框的字符在Android和Windows平台上显示为彩色:
201809132105040401
阅读(998) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~