检测电话号码&链接xamarin.Form

时间:2017-07-04 14:09:44

标签: xamarin xamarin.forms

假设我们有以下文字:

  

请致电015546889或发送电子邮件至email@hotmail.com

与我们联系

如何在xamarin.forms的同一标签中显示上述文字,并通过发送电子邮件处理点击电子邮件,并点击该号码处理电话。

我可以使用以下代码制作可点击标签

Label label = new Label;
     label.GestureRecognizers.Add(new TapGestureRecognizer()
    {
         Command = new Command(() => {
           //do some function here
        })
   });

如何与消息传递应用程序或Whatsapp应用程序进行超链接

2 个答案:

答案 0 :(得分:1)

查看Forms9Patch中的Label元素。它有一个HtmlText属性,允许简单的标记。

using System;
using Xamarin.Forms;
namespace Forms9PatchDemo
{
    public class LabelLink : ContentPage
    {
        public LabelLink()
        {
            var label = new Forms9Patch.Label
            {
                HtmlText = "Contact us on <a id=\"phone\" href=\"tel:+353015546889\">015546889</a> or <a id=\"email\" href=\"mailto:email@hotmail.com\">email@hotmail.com</a>"
            };
            label.ActionTagTapped += (object sender, Forms9Patch.ActionTagEventArgs e) =>
            {
                var id = e.Id;
                var href = e.Href;
                var uri = new Uri(e.Href);
                Device.OpenUri(uri);
            };
            Content = new StackLayout
            {
                VerticalOptions = LayoutOptions.Center,
                Children = {
                    new Label { Text = "Forms9Patch.Label.HtmlText <a> example" },
                    new BoxView { BackgroundColor = Color.Black, HeightRequest = 1 },
                    label
                }
            };
        }
    }
}

请注意,上述示例无法在iOS模拟器上运行,因为不支持tel:mailto:方案。它适用于实际的iOS设备。

答案 1 :(得分:1)

经过大量搜索,我找到了完美的解决方案 https://theconfuzedsourcecode.wordpress.com/tag/xamarin-hyperlink-label/

希望这会有所帮助:)