• Xamarin.Forms中实现CheckBox控件


    Xamarin.Forms中实现CheckBox控件

    由于Xamarin.Forms中没有Checkbox这个基础控件,我们就只能自己来实现啦!

    这里采用的是继承Image来实现Checkbox控件,代码如下所示:

    IconUnChecked :未选中状态的图片名称

    IconChecked:选中状态的图片名称

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Text;
    using Xamarin.Forms;
    
    namespace AppTest.CustomView
    {
        public class Checkbox : Image
        {
    
            private const string CheckboxUnCheckedImage = "IconUnChecked";
            private const string CheckboxCheckedImage = "IconChecked";
    
    
            public Checkbox()
            {
                Source = CheckboxUnCheckedImage;
                var imageTapGesture = new TapGestureRecognizer();
                imageTapGesture.Tapped += ImageTapGestureOnTapped;
                GestureRecognizers.Add(imageTapGesture);
                PropertyChanged += OnPropertyChanged;
            }
    
            private void ImageTapGestureOnTapped(object sender, EventArgs eventArgs)
            {
                if (IsEnabled)
                {
                    Checked = !Checked;
                }
            }
    
            /// <summary>
            /// The checked changed event.
            /// </summary>
            public event EventHandler<bool> CheckedChanged;
    
            /// <summary>
            /// The checked state property.
            /// </summary>
            public static readonly BindableProperty CheckedProperty = BindableProperty.Create("Checked", typeof(bool), typeof(Checkbox), false, BindingMode.TwoWay, propertyChanged: OnCheckedPropertyChanged);
    
            public bool Checked
            {
                get
                {
                    return (bool)GetValue(CheckedProperty);
                }
    
                set
                {
                    if (Checked != value)
                    {
                        SetValue(CheckedProperty, value);
                        CheckedChanged?.Invoke(this, value);
                    }
                }
            }
    
            private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                if (e?.PropertyName == IsEnabledProperty.PropertyName)
                {
                    Opacity = IsEnabled ? 1 : 0.5;
                }
            }
    
            private static void OnCheckedPropertyChanged(BindableObject bindable, object oldValue, object newValue)
            {
                var checkBox = bindable as Checkbox;
                if (checkBox != null)
                {
                    var value = newValue as bool?;
                    checkBox.Checked = value.GetValueOrDefault();
                    checkBox.Source = value.GetValueOrDefault() ? CheckboxCheckedImage : CheckboxUnCheckedImage;
                }
            }
    
    
    
        }
    }
  • 相关阅读:
    中国的南方人和北方人有什么区别?总算说透了!
    怎样通过穴位按摩来减轻脚踝扭伤的疼痛
    关于脚踝不得不说的各种事
    电影发烧友必备知识-720P、1080P、4K的区别
    有什么相见恨晚的小知识?
    男生有钱到底有多重要?
    es6学习笔记5--promise
    es6学习笔记4--数组
    js设计模式总结1
    es6学习笔记3--解构和对象
  • 原文地址:https://www.cnblogs.com/devin_zhou/p/8550494.html
Copyright © 2020-2023  润新知