This is my hyperlink control. I can use some generic style on it. This control must have the appearance of a classic html hyperlink. By default it behaves like a button. But it can also be used to open the default mailing app of the device. This control allows you to open a phone number in the dial screen. Last, this control can be used to open a location on the default map application of the device.

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

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

    #endregion

    /// <summary>
    /// All kind of link types.
    /// </summary>
    public enum TypesLink
    {
        /// <summary>
        /// The default link : execute the command binded.
        /// </summary>
        Default = 0,

        /// <summary>
        /// The email.
        /// </summary>
        Email,

        /// <summary>
        /// The phone number.
        /// </summary>
        PhoneNumber,

        /// <summary>
        /// The location.
        /// </summary>
        Location
    }

    /// <summary>
    /// “Xamarin.Forms.Label” extended to get a kind of hyperlink.
    /// </summary>
    public class ExtHyperLink : Label
    {
        #region Bindable property

        /// <summary>
        /// The type link property.
        /// </summary>
        [SuppressMessage(StyleCop.CSharp.MaintainabilityRules, SA1401:FieldsMustBePrivate, Justification = Reviewed.)]
        public static BindableProperty TypeLinkProperty = BindableProperty.Create<ExtHyperLink, TypesLink>(x => x.TypeLink, TypesLink.Default);

        /// <summary>
        /// The command clicked property.
        /// </summary>
        [SuppressMessage(StyleCop.CSharp.MaintainabilityRules, SA1401:FieldsMustBePrivate, Justification = Reviewed.)]
        public static BindableProperty CommandProperty = BindableProperty.Create<ExtHyperLink, ICommand>(x => x.Command, null);

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref=ExtHyperLink/> class.
        /// </summary>
        public ExtHyperLink()
            : base()
        {
            this.FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Xamarin.Forms.Label));
            this.LineBreakMode = Xamarin.Forms.LineBreakMode.TailTruncation;
            this.SubscribeClickToRaiseCommand();
        }

        #endregion

        #region Property

        /// <summary>
        /// Gets or sets the type link.
        /// </summary>
        /// <value>The type link.</value>
        public TypesLink TypeLink
        {
            get { return (TypesLink)this.GetValue(TypeLinkProperty); }
            set { this.SetValue(TypeLinkProperty, value); }
        }

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

        #endregion

        /// <summary>
        /// Subscribes the click to raise command.
        /// </summary>
        private void SubscribeClickToRaiseCommand()
        {
            var tapGestureRecognizer = new TapGestureRecognizer();
            tapGestureRecognizer.Tapped += (sender, e) =>
            {
                if (this.TypeLink == TypesLink.Default)
                {
                    if (this.Command != null)
                    {
                        Command.Execute(null);
                    }
                }
                else if (this.TypeLink == TypesLink.PhoneNumber)
                {
                    string numTel = this.Text.Replace(., string.Empty);
                    numTel = numTel.Replace( , string.Empty);
                    numTel = numTel.Replace(, string.Empty);
                    Device.OpenUri(new Uri(tel:// + numTel));
                }
                else if (this.TypeLink == TypesLink.Email)
                {
                    Device.OpenUri(new Uri(mailto: + this.Text));
                }
                else if (this.TypeLink == TypesLink.Location)
                {
                    Device.OpenUri(new Uri(geo: + this.Text));
                }
                else
                {
                    throw new NotImplementedException(SubscribeClickToRaiseCommand > TypeLink not implemented);
                }
            };
            this.GestureRecognizers.Add(tapGestureRecognizer);
        }
    }
}

Leave a Reply

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