ExtSwitch

My custom switch control. This control is kind of useless right now. I don’t use to bind a command on a switch. Even more if the switch is used in a ListView because I may handle the tap event on a bigger area than just the switch itself.

//
// <copyright file=ExtSwitch.cs company=Krossapp>
//     Copyright (c) Krossapp. All rights reserved.
// </copyright>
// <author>Nicolas Krier</author>
//
namespace SDF.XForm.Controls
{
    #region Using

    using System.Diagnostics.CodeAnalysis;
    using System.Windows.Input;
    using Xamarin.Forms;

    #endregion

    /// <summary>
    /// The Switch.
    /// </summary>
    public class ExtSwitch : Switch
    {
        #region Bindable property

        /// <summary>
        /// The command toggled property.
        /// </summary>
        [SuppressMessage(StyleCop.CSharp.MaintainabilityRules, SA1401:FieldsMustBePrivate, Justification = Reviewed.)]
        public static BindableProperty CommandToggledProperty = BindableProperty.Create<ExtSwitch, ICommand>(x => x.CommandToggled, null);

        /// <summary>
        /// The command parameter toggled property.
        /// </summary>
        [SuppressMessage(StyleCop.CSharp.MaintainabilityRules, SA1401:FieldsMustBePrivate, Justification = Reviewed.)]
        public static BindableProperty CommandToggledParameterProperty = BindableProperty.Create<ExtSwitch, object>(x => x.CommandToggledParameter, null);

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref=ExtSwitch/> class.
        /// </summary>
        public ExtSwitch()
            : base()
        {
            // SubscribeClickToRaiseCommand();
            this.Toggled += this.OnItemSelected;
        }

        #endregion

        #region Property

        /// <summary>
        /// Gets or sets the command toggled.
        /// </summary>
        /// <value>The command toggled.</value>
        public ICommand CommandToggled
        {
            get { return (ICommand)this.GetValue(CommandToggledProperty); }
            set { this.SetValue(CommandToggledProperty, value); }
        }

        /// <summary>
        /// Gets or sets the command parameter toggled.
        /// </summary>
        /// <value>The command parameter toggled.</value>
        public object CommandToggledParameter
        {
            get { return (object)this.GetValue(CommandToggledParameterProperty); }
            set { this.SetValue(CommandToggledParameterProperty, value); }
        }

        #endregion

        /// <summary>
        /// Raises the item selected event.
        /// </summary>
        /// <param name=sender>The sender.</param>
        /// <param name=e>The event args</param>
        private void OnItemSelected(object sender, ToggledEventArgs e)
        {
            if (this.CommandToggled != null && sender.GetType() == typeof(ExtSwitch) && ((ExtSwitch)sender).CommandToggledParameter != null && this.CommandToggled.CanExecute(e) && e.Value)
            {
                this.CommandToggled.Execute(this.CommandToggledParameter);
            }
        }

        /// <summary>
        /// Subscribes the click to raise command.
        /// </summary>
        private void SubscribeClickToRaiseCommand()
        {
            var tapGestureRecognizer = new TapGestureRecognizer();
            tapGestureRecognizer.Tapped += (sender, e) =>
            {
                if (this.CommandToggled != null && this.CommandToggled.CanExecute(e))
                {
                    this.CommandToggled.Execute(this.CommandToggledParameter);
                }
            };
            this.GestureRecognizers.Add(tapGestureRecognizer);
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *